安卓 : Loading an image from the Web with Asynctask

标签 android android-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(Bitmap result) 中传递对 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));

有没有简单的解决方案? 我这里有什么问题吗?

最佳答案

如果没有充分的理由自己下载图像,那么我建议使用 Picasso .

Picasso 为您省去了下载、设置和缓存图像的所有问题。 一个简单示例所需的整个代码是:

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

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


如果图像不是那么大,您可以使用匿名类来执行异步任务。 这会是这样的:

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

mChart.setTag(URL);
new DownloadImageTask.execute(mChart);

任务类:

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) {
   ...
}

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

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

关于安卓 : Loading an image from the Web with Asynctask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3090650/

相关文章:

java - 获取AsyncTask的结果而不阻塞线程

java - AsyncTask 在运行期间不显示对话框

java - 没有互联网连接时应用程序崩溃

android - 从 onhandleintent 启动异步任务

android - 将受信任的 SSL 证书添加到为 API 8 设计的应用程序

android - react native : Unable to download JS bundle from the Dev Server

android - 重用线程

android - 应用程序缓存 iOS PhoneGap

android - 获得插图的正确方法

Android - 使用 AsyncTasks 的正确方法?