使用的AsyncTask的RecyclerView下载图片在安卓 [英] Downloading Image in android using asynctask for RecyclerView

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

问题描述

这我下面的AsyncTask 类code下载图像 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);
                }
            }
        }
    }

我称之为的AsyncTask 在我的适配器是这样的:

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

该应用程序崩溃我每次运行它​​。该网址下载图像是一个的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);
}

下面是我用code和它完美的作品

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

}

这里是我如何从称它为我的适配器

And here is how i call it from my ADAPTER

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

单独为每个URL。

seperately for every URL.

试试这个,如果它可以帮助你。

Try this if it helps you out.

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

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