I made the AsyncTask to load images for my RecyclerView. It has method downloadImage() which I call every time at ViewHolder. So for each image it should create new AsyncTask? I can't figure out if it download parallel or sequentially. (I can't use libraries, all must be custom)
private static class DownloadImage extends AsyncTask<String, Void, Bitmap> {
private ImageView mBitmapImage;
DownloadImage(ImageView imageView) {
mBitmapImage = imageView;
}
@Override
protected Bitmap doInBackground(String... strings) {
String url = strings[0];
Bitmap bitmapImage = null;
InputStream in = null;
try {
in = new URL(url).openStream();
bitmapImage = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bitmapImage;
}
@Override
protected void onPostExecute(Bitmap result) {
if (result != null) {
mBitmapImage.setImageBitmap(result);
} else {
mBitmapImage.setImageResource(R.drawable.loading_movie);
}
}
}
public static void downloadImage(String imageLocation, ImageView imageView) {
new DownloadImage(imageView).execute(MOVIE_POSTER_URL_REQUEST + imageLocation);
}
In adapter I call it like this:
void bindMovie(Movie movie) {
mMovie = movie;
mMovieTitle.setText(movie.getTitle());
mDescription.setText(movie.getOverview());
downloadImage(movie.getPosterPath(), mPoster);
}