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)

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Could you please provide some code example to illustrate?
What is the source of image. Are you getting it from a link or from resources
I have updated the code , please see updated question.
I am getting the image from url.
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.