Android:使用Asynctask从Web上加载图片

如何用Asynctaskreplace以下代码行? 你如何从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; //--------------------------------------------------- } 

我想过这样的事情:

replace:

 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以某种方式传递它吗? 我想代替我所有的代码行:

 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)); 

有这个简单的解决scheme吗? 我在这里弄错了什么?

如果没有足够的理由自己下载图像,那么我会build议使用毕加索 。

毕加索为您节省下载,设置和caching图像的所有问题。 整个代码需要一个简单的例子是:

 Picasso.with(context).load(url).into(imageView); 

如果你真的想自己做所有事情,请使用下面的旧的答案。


如果图像不是那么大,你可以使用匿名类作为asynchronous任务。 这是这样的:

 ImageView mChart = (ImageView) findViewById(R.id.imageview); String URL = "http://www...anything ..."; mChart.setTag(URL); new DownloadImageTask.execute(mChart); 

Task类:

 public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> { ImageView imageView = null; @Override protected Bitmap doInBackground(ImageView... imageViews) { this.imageView = imageViews[0]; return download_Image((String)imageView.getTag()); } @Override protected void onPostExecute(Bitmap result) { imageView.setImageBitmap(result); } private Bitmap download_Image(String url) { ... } 

将标签隐藏在标签中有点棘手,但是如果您有很多想要以这种方式填充的图像视图,则在调用类中看起来会更好。 如果您在ListView中使用ImageView并且想要知道在下载图像期间ImageView是否被回收,这也有帮助。

我写了如果你的图像不是那么大,因为这将导致任务有一个隐含的指针的基本活动,导致垃圾收集器保存整个活动在内存中,直到任务完成。 如果用户在下载位图时移动到应用程序的另一个屏幕,则不能释放内存,这可能会使您的应用程序和整个系统变慢。

试试这个代码:

 ImageView myFirstImage = (ImageView) findViewById(R.id.myFirstImage); ImageView mySecondImage = (ImageView) findViewById(R.id.mySecondImage); ImageView myThirdImage = (ImageView) findViewById(R.id.myThirdImage); String URL1 = "http://www.google.com/logos/2013/estonia_independence_day_2013-1057005.3-hp.jpg"; String URL2 = "http://www.google.com/logos/2013/park_su-geuns_birthday-1055005-hp.jpg"; String URL3 = "http://www.google.com/logos/2013/anne_cath_vestlys_93rd_birthday-1035005-hp.jpg"; myFirstImage.setTag(URL1); mySecondImage.setTag(URL2); myThirdImage.setTag(URL3); new DownloadImageTask.execute(myFirstImage); new DownloadImageTask.execute(mySecondImage); new DownloadImageTask.execute(myThirdImage); public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> { ImageView imageView = null; @Override protected Bitmap doInBackground(ImageView... imageViews) { this.imageView = imageViews[0]; return download_Image((String)imageView.getTag()); } @Override protected void onPostExecute(Bitmap result) { imageView.setImageBitmap(result); } private Bitmap download_Image(String url) { Bitmap bmp =null; try{ URL ulrn = new URL(url); HttpURLConnection con = (HttpURLConnection)ulrn.openConnection(); InputStream is = con.getInputStream(); bmp = BitmapFactory.decodeStream(is); if (null != bmp) return bmp; }catch(Exception e){} return bmp; } } 

你可以创build一个类say..BkgProcess,它包含一个扩展AsyncTask的内部类。 而实例化BkgProcess在BkgProcess构造函数中传递Activity类的上下文。 例如:

 public class BkgProcess { String path; Context _context; public Download(Downloader downloader, String path2){ this.path = path2; _context = downloader; } public void callProgressDialog(){ new BkgProcess().execute((Void)null); } class Downloads extends AsyncTask<Void, Void, Boolean> { private ProgressDialog dialog = new ProgressDialog(_context); protected void onPreExecute(){ dialog.setMessage("Downloading image.."); dialog.show(); } protected void onPostExecute(Boolean success) { dialog.dismiss(); if(success) Toast.makeText(_context, "Download complete", Toast.LENGTH_SHORT).show(); } @Override protected Boolean doInBackground(Void... params) { return(startDownload(path)); } public boolean startDownload(String img_url) { // download img.. return true; } } } 

来自你的活动课

 BkgProcess dwn = new BkgProcess (Your_Activity_class.this, img_path); dwn.callProgressDialog(); 

这将得到任何大小的图像…如果你不想进度对话框只是评论onPreExecute();

 for(int i = 0 ; i < no_of_files ; i++ ) new FetchFilesTask().execute(image_url[i]); private class FetchFilesTask extends AsyncTask<String, Void, Bitmap> { private ProgressDialog dialog = new ProgressDialog(FileExplorer.this); Bitmap bitmap[]; protected void onPreExecute(){ dialog.setMessage("fetching image from the server"); dialog.show(); } protected Bitmap doInBackground(String... args) { bitmap = getBitmapImageFromServer(); return bitmap; } protected void onPostExecute(Bitmap m_bitmap) { dialog.dismiss(); if(m_bitmap != null) //store the images in an array or do something else with all the images. } } public Bitmap getBitmapImageFromServer(){ // fetch image form the url using the URL and URLConnection class }