3

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)

Community
  • 1
  • 1
casillas
  • 16,351
  • 19
  • 115
  • 215

1 Answers1

2

Because you are recycle bitmap when user press the image. If you recycle the image you can assign it to null. After you can check the image is null. If it is null you should initialise again.

UPDATE:

 imageLoader.get(newsItem.getThumbUrl(), new ImageLoader.ImageListener() {
        @Override
        public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {

        }

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

Use this code for getting the bitmap store it as global and recycle the global bitmap. you can get the bitmap with response.getBitmap() function. After getting the bitmap you can set it to image view. Every time you want to load the image this function loaded as mutable bitmap and you can recycle it.

kgnzpk
  • 108
  • 11
  • Could you please provide some code example to illustrate? – casillas Jan 12 '16 at 21:22
  • What is the source of image. Are you getting it from a link or from resources – kgnzpk Jan 12 '16 at 21:24
  • I have updated the code , please see updated question. – casillas Jan 12 '16 at 21:25
  • I am getting the image from url. – casillas Jan 12 '16 at 21:29
  • 1
    I think the problem is about cacheing because volley cache loaded images and you are recycle the image from cache just delete the recycle command click the image see the memory. after than click it the same image and see the memory allocates more or stay same values. You – kgnzpk Jan 12 '16 at 21:34
  • could you please let me know what I am supposed to put in the onResponse? how should I clear the cache in the volley? I have upvoted and marked your answer. – casillas Jan 12 '16 at 23:18
  • You need to get Bitmap from response and you should set this bitmap to networkImageView or regular imageview – kgnzpk Jan 13 '16 at 01:15