java - 使用 RecyclerView 的 asynctask 在 android 中下载图像

标签 java android android-asynctask android-recyclerview

这是我的以下 AsyncTask 类代码,用于为 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);
                }
            }
        }
    }

我这样在我的适配器中调用AsyncTask:

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

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

我猜我在调用 AsyncTask 时犯了这个错误,但我想不出解决方案。

最佳答案

改变这个

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

对此

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

这是我使用的代码,效果很好

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。

如果对您有帮助,请尝试一下。

关于java - 使用 RecyclerView 的 asynctask 在 android 中下载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31041100/

相关文章:

java - 编译器不会提示我捕获了一个永远不会抛出的异常

android - ListView 很慢-android

java - 关于 setter和getter 的问题

java - headless Chrome 可以使用终端,但不能使用设置参数

java - Android Icon/Image设置模式多种选择

android - 回收器 View : No adapter attached; skipping layout (can't find issue)

Android:如何通过点击一个按钮从右到左和左右移动跑马灯文字

javascript - Android Phonegap : Notify javascript when an AsyncTask is finished

android - 部分信任环境中的异步 CTP

java - 如何将HashMap中的所有值相加?