Basically, I have a main activity which is grid view has images, and when user clicks on an image, it shows the corresponding detail activity. Everything works fine and functional, but it keeps images in the memory and I am trying to release the memory by using bitmap.recycle();
based on the following link, by implementing that method, out of memory issue has fixed a little bit. But it still crashes in the following scenario.
Let say user click on image 1 and then detail activity comes, and click on the back button and click on the same image again, then while opening the detail activity it crashes. But if user clicks on the different image, it does not crash.
ImageView initialization
@Override
protected void onResume() {
super.onResume();
productImageView = (NetworkImageView) findViewById(R.id.detailImage);
ImageLoader imageLoader = CustomVolleyRequest.getInstance(this.getApplicationContext())
.getImageLoader();
imageLoader.get(url, ImageLoader.getImageListener(productImageView,
R.drawable.image, android.R.drawable
.ic_dialog_alert));
productImageView.setImageUrl(url, imageLoader);
}
Back Button Pressed
@Override
public void onBackPressed() {
Intent intent = new Intent(ProductDetailActivity.this,MainActivity.class);
ProductDetailActivity.this.startActivity(intent);
this.finish();
return;
}
@Override
public void onDestroy() {
super.onDestroy();
Drawable drawable = productImageView.getDrawable();
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
bitmap = null;
}
}
System.gc();
}
Here is the crash report.
java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@42524248 E/AndroidRuntime: at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1084) E/AndroidRuntime: at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:844) E/AndroidRuntime: at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:490) E/AndroidRuntime: at android.widget.ImageView.onDraw(ImageView.java:1037) E/AndroidRuntime:at android.view.View.draw(View.java:14506)