Recycler Networkimageview Scrolling Back And Forward, Out Of Memory And Resizing Issues
I am working to add an image from the gallery horizontally using recycler view as follows. I could able to add images next to each other. The following code works but crashes somet
Solution 1:
You should scale your Bitmap and use caching for reducing loading time. Here on this repository I've put reference code for approaching image loading with scaling and caching.
I report here code to be used for setPic method. This essentially uses an LruCache for improving Bitmap loading while scrolling. This could be a good starting point. Notice also that Bitmap are scaled before putting them in cache.
privatestaticfinalintWIDTH=100;
private LruCache<String, Bitmap> mMemoryCache;
publicvoidsetPic(MyImageView view, String url) {
Contextcontext= view.getContext();
if (!TextUtils.isEmpty(url)) {
if (url.startsWith("http")) {
view.setImageUrl(url, VolleyHandler.getInstance(context).getImageLoader());
} else {
try {
Uriuri= Uri.parse(url);
Bitmapbitmap= mMemoryCache.get(url);
if (bitmap == null) {
InputStreamis= context.getContentResolver().openInputStream(uri);
bitmap = BitmapFactory.decodeStream(is);
Bitmapscaled= ImageUtils.getInstance().scaleBitmap(bitmap);
mMemoryCache.put(url, scaled);
if (is!=null) {
is.close();
}
}
view.setLocalImageBitmap(bitmap);
} catch (Exception ex) {
Log.e("Image", ex.getMessage(), ex);
}
}
} else {
view.setImageUrl("", VolleyHandler.getInstance(view.getContext()).getImageLoader());
}
}
public Bitmap scaleBitmap(Bitmap bitmap) {
intwidth= WIDTH;
intheight= (WIDTH * bitmap.getHeight()) / bitmap.getWidth();
return Bitmap.createScaledBitmap(bitmap, width, height, false);
}
I suggest also to refer to Android Developers documentation here (for efficient Bitmap loading) and here (for Bitmap caching with Volley).
Post a Comment for "Recycler Networkimageview Scrolling Back And Forward, Out Of Memory And Resizing Issues"