使用 asynctask 为 RecyclerView 在 android 中下载图像 [英] Downloading Image in android using asynctask for RecyclerView

查看:31
本文介绍了使用 asynctask 为 RecyclerView 在 android 中下载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的以下 AsyncTask 类代码,用于下载 RecyclerView 的图像.

This my following AsyncTask class code for downloading image for RecyclerView.

public class MyDownloadImageAsyncTask extends AsyncTask<String, Void,Bitmap> {
    private final WeakReference<ImageView> imageViewReference;

    public MyDownloadImageAsyncTask(ImageView imv) {
        imageViewReference = new WeakReference<ImageView>(imv);
    }

    @Override
    protected Bitmap doInBackground(String... urls) {
        Bitmap bitmap = null;
        for (String url : urls) {
            bitmap = MyUtility.downloadImage(url);
            /*if (bitmap != null) {
                mImgMemoryCache.put(url, bitmap);
            }*/
        }
        return bitmap;
    }

    protected void onPostExecute(Bitmap bitmap){
        if (imageViewReference != null && bitmap != null) {
            final ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
                }
            }
        }
    }

我以这种方式调用 Adapter 中的 AsyncTask:

I call the AsyncTask in my Adapter this way:

MyDownloadImageAsyncTask task = new MyDownloadImageAsyncTask(holder.vIcon);
task.execute(new String[] {(String)movie.get("image")}););

每次运行该应用程序时都会崩溃.用于下载图像的 URL 位于 ArrayList 中.

The app is crashing every time I run it. The URL for downloading the image is in a ArrayList.

我猜我在调用 AsyncTask 时犯了这个错误,但我想不出解决办法.

I guess the mistake I'm doing this in calling the AsyncTask but I couldn't figure out the solution.

推荐答案

改变这个

 public MyDownloadImageAsyncTask(ImageView imv) {
        imageViewReference = new WeakReference(imv);
    }

到这里

public MyDownloadImageAsyncTask(ImageView imv) {
    imageViewReference = new WeakReference<ImageView>(imv);
}

这是我使用的代码,它运行完美

Here is the code that i use and it works perfect

class LoadImage extends AsyncTask<String, Void, Bitmap> {

private final WeakReference<ImageView> imageViewReference;

public LoadImage(ImageView imageView) {
    imageViewReference = new WeakReference<ImageView>(imageView);
}

@Override
protected Bitmap doInBackground(String... params) {
    try {
        return downloadBitmap(params[0]);
    } catch (Exception e) {
       // log error
    }
    return null;
}

@Override
protected void onPostExecute(Bitmap bitmap) {
    if (isCancelled()) {
        bitmap = null;
    }

    if (imageViewReference != null) {
        ImageView imageView = imageViewReference.get();
        if (imageView != null) {
            if (bitmap != null) {
                imageView.setImageBitmap(bitmap);
            } else {
                Drawable placeholder = imageView.getContext().getResources().getDrawable(R.drawable.ic_launcher);
                imageView.setImageDrawable(placeholder);
            }
        }
    }
}

private Bitmap downloadBitmap(String url) {
    HttpURLConnection urlConnection = null;
    try {
        URL uri = new URL(url);
        urlConnection = (HttpURLConnection) uri.openConnection();
        int statusCode = urlConnection.getResponseCode();
        if (statusCode != HttpStatus.SC_OK) {
            return null;
        }

        InputStream inputStream = urlConnection.getInputStream();
        if (inputStream != null) {
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            return bitmap;
        }
    } catch (Exception e) {
        urlConnection.disconnect();
        Log.w("ImageDownloader", "Error downloading image from " + url);
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
    return null;
}

}

这是我从我的 ADAPTER

new LoadImage(holder.itemImage).execute(IMAGE_URL);

单独用于每个 URL.

seperately for every URL.

如果它对你有帮助,试试这个.

Try this if it helps you out.

这篇关于使用 asynctask 为 RecyclerView 在 android 中下载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆