如何更換的代碼用的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