如何更换的代码用的AsyncTask以下行? 你如何从Asynctask中“取回”位图?谢谢。安卓:从网站加载图像用的AsyncTask
ImageView mChart = (ImageView) findViewById(R.id.Chart);
String URL = "http://www...anything ...";
mChart.setImageBitmap(download_Image(URL));
public static Bitmap download_Image(String url) {
//---------------------------------------------------
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e("Hub","Error getting the image from server : " + e.getMessage().toString());
}
return bm;
//---------------------------------------------------
}
我想过这样的事情:
取代:
mChart.setImageBitmap(download_Image(graph_URL));
通过类似:
mChart.setImageBitmap(new DownloadImagesTask().execute(graph_URL));
和
public class DownloadImagesTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... urls) {
return download_Image(urls[0]);
}
@Override
protected void onPostExecute(Bitmap result) {
mChart.setImageBitmap(result); // how do I pass a reference to mChart here ?
}
private Bitmap download_Image(String url) {
//---------------------------------------------------
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e("Hub","Error getting the image from server : " + e.getMessage().toString());
}
return bm;
//---------------------------------------------------
}
}
,但我如何通过在onPostExecute(位图结果)mChart参考??? 我需要以某种方式将URL传递给URL吗? 我想取代我的所有代码行:
mChart1.setImageBitmap(download_Image(URL_1));
mChart2.setImageBitmap(download_Image(URL_2));
有类似的东西...但在这样的AsyncTask!
mChart1.setImageBitmap(new DownloadImagesTask().execute(graph_URL_1));
mChart2.setImageBitmap(new DownloadImagesTask().execute(graph_URL_2));
有没有简单的解决方案呢? 我在这里遇到什么问题吗?
很基本的问题,但将是有益的许多beginers像我一样,我认为...请发布一些代码,如果你有。 – Hubert 2010-06-22 06:13:01
相关:[ImageDownloader示例](http://developer.android.com/resources/samples/XmlAdapters/src/com/example/android/xmladapters/ImageDownloader.html) – idbrii 2011-07-14 21:23:27
@Hubert可以提及私人位图download_Image( String url){ ... }这段代码? – Erum 2015-03-27 12:48:38