Downloading Image in android using asynctask for RecyclerView(使用 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 中下载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 asynctask 为 RecyclerView 在 android 中下载图像
				
        
 
            
        基础教程推荐
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
 - Java Swing计时器未清除 2022-01-01
 - 在 Java 中创建日期的正确方法是什么? 2022-01-01
 - 不推荐使用 Api 注释的描述 2022-01-01
 - 验证是否调用了所有 getter 方法 2022-01-01
 - 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
 - 多个组件的复杂布局 2022-01-01
 - Java 实例变量在两个语句中声明和初始化 2022-01-01
 - 从 python 访问 JVM 2022-01-01
 - 大摇大摆的枚举 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				