Canvas: trying to use a recycled bitmap android.graphics.Bitmap in Android

后端 未结 8 1700
忘掉有多难
忘掉有多难 2020-12-13 17:32

I am working on the crop image class, but encounter a recycled bit map problem:

03-02 23:14:10.514: E/AndroidRuntime(16736): FATAL EXCEPTION: Thread-1470
03-         


        
相关标签:
8条回答
  • 2020-12-13 17:43

    In my case, i have inflate a layout containing imageview with src image where i have faced same error. That case, problem could be resolved by adding source image programmatically like:

    ((ImageView)view.findViewById(R.id.imageview)).setImageBitmap(BitmapFactory.decodeResource(getContext().getResources(),
                    R.drawable.testimage));
    
    0 讨论(0)
  • 2020-12-13 17:45

    For those that did not find a solution so far. I had the same problem. I tried to recycle a bitmap in onPause like this:

    final Drawable drawable = mImageView.getDrawable();
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        Bitmap bitmap = bitmapDrawable.getBitmap();
        bitmap.recycle();
    }
    
    if (preView != null && !preView.isRecycled()) {
        preView.recycle();
        preView = null;
    }
    

    After returning back i got the exception: "Canvas: trying to use a recycled bitmap"

    Solution for me: I had to add the following

    mImageView.setImageBitmap(null);
    
    0 讨论(0)
  • 2020-12-13 17:49

    Try to add this before calling recycle() methods to make sure bitmap isn't already recycled:

    if (mBitmap != null && !mBitmap.isRecycled()) {
        mBitmap.recycle();
        mBitmap = null; 
    }
    
    0 讨论(0)
  • 2020-12-13 17:54

    Probably the image/png files which you are using have bigger dimensions then 512pixels e.g width or height is greater than 512pixels. Try to decrease the dimensions to at least 512 pixels. In my case I was also getting same error like E/BitmapDrawable: Canvas: trying to use a recycled bitmap.

    By using above described way, I was able to solve the error in a day.

    Keynote 512 pixels maybe not exact value for threshold, so take a look at your logcat too.

    0 讨论(0)
  • 2020-12-13 17:59

    This should fix the issue in accordance to the android documentation here : https://developer.android.com/topic/performance/graphics/manage-memory#java Here's the code just incase the link doesn't work .

        private int cacheRefCount = 0;
    private int displayRefCount = 0;
    ...
    // Notify the drawable that the displayed state has changed.
    // Keep a count to determine when the drawable is no longer displayed.
    public void setIsDisplayed(boolean isDisplayed) {
        synchronized (this) {
            if (isDisplayed) {
                displayRefCount++;
                hasBeenDisplayed = true;
            } else {
                displayRefCount--;
            }
        }
        // Check to see if recycle() can be called.
        checkState();
    }
    
    // Notify the drawable that the cache state has changed.
    // Keep a count to determine when the drawable is no longer being cached.
    public void setIsCached(boolean isCached) {
        synchronized (this) {
            if (isCached) {
                cacheRefCount++;
            } else {
                cacheRefCount--;
            }
        }
        // Check to see if recycle() can be called.
        checkState();
    }
    
    private synchronized void checkState() {
        // If the drawable cache and display ref counts = 0, and this drawable
        // has been displayed, then recycle.
        if (cacheRefCount <= 0 && displayRefCount <= 0 && hasBeenDisplayed
                && hasValidBitmap()) {
            getBitmap().recycle();
        }
    }
    
    private synchronized boolean hasValidBitmap() {
        Bitmap bitmap = getBitmap();
        return bitmap != null && !bitmap.isRecycled();
    }
    
    0 讨论(0)
  • 2020-12-13 17:59

    for me the problem was i was using an arraylist to store all the bitmap values inorder to parse them into an adapter, what i was doing was that i was resizing the bitmap and then re orienting them ( the same bitmap file ) and adding them both in the arraylist

    SOLUTION Created a temp instance of Bitmap and resized it and then reoriented the temp one and stored the second one in the arraylist and parsed to the adapter

    0 讨论(0)
提交回复
热议问题