简体   繁体   中英

Downloading Image in android using asynctask for 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);
                }
            }
        }
    }

I call the AsyncTask in my Adapter this way:

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

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

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

Change this

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

to this

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

}

And here is how i call it from my ADAPTER

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

seperately for every URL.

Try this if it helps you out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM