fresco icon indicating copy to clipboard operation
fresco copied to clipboard

Crash: trying to use a recycled bitmap

Open nikslushkin opened this issue 11 years ago • 6 comments

Reproduced when I switching between screens while images are loading.

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@1157c7bd at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1225) at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:600) at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:537) at com.facebook.drawee.drawable.ForwardingDrawable.draw(ForwardingDrawable.java:139) at com.facebook.drawee.drawable.ForwardingDrawable.draw(ForwardingDrawable.java:139) at com.facebook.drawee.drawable.ScaleTypeDrawable.draw(ScaleTypeDrawable.java:104) at com.facebook.drawee.drawable.FadeDrawable.drawDrawableWithAlpha(FadeDrawable.java:306) at com.facebook.drawee.drawable.FadeDrawable.draw(FadeDrawable.java:293) at com.facebook.drawee.generic.GenericDraweeHierarchy$RootFadeDrawable.draw(GenericDraweeHierarchy.java:127) at android.widget.ImageView.onDraw(ImageView.java:1148) at android.view.View.draw(View.java:15114) at android.view.View.updateDisplayListIfDirty(View.java:14048) at android.view.View.getDisplayList(View.java:14071) at android.view.View.draw(View.java:14838)

nikslushkin avatar May 05 '15 14:05 nikslushkin

Can you provide a snippet of your code and XML, if you are using it?

tyronen avatar May 05 '15 14:05 tyronen

I have ViewPagers in every item of RecyclerView. When I scroll views sometimes app crashes. I found out that cause of the crash is setting image from cache.

public void loadImage(long hotelId) {
    final String url = getImageUrl(hotelId, mImageSize);
    ImageRequest request = ImageRequest.fromUri(url);
    mImage.getHierarchy().reset();

    ImagePipeline imagePipeline = Fresco.getImagePipeline();

    boolean imageSet = setImageFromCache(request, imagePipeline);
    if (!imageSet) {
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(mImage.getController())
                .build();
        mImage.setController(controller);
    }
}

private boolean setImageFromCache(ImageRequest request, ImagePipeline imagePipeline) {
    boolean imageSet = false;
    DataSource<CloseableReference<CloseableImage>> dataSource =
            imagePipeline.fetchImageFromBitmapCache(request, null);
    try {
        CloseableReference<CloseableImage> imageReference = dataSource.getResult();
        if (imageReference != null) {
            try {
                Drawable drawable = createDrawable(imageReference);
                if (drawable != null) {
                    imageSet = true;
                    mImage.setController(null);
                    mImage.getHierarchy().setImage(drawable, 10000, true);
                }
            } finally {
                CloseableReference.closeSafely(imageReference);
            }
        }
    } finally {
        dataSource.close();
    }
    return imageSet;
}

private Drawable createDrawable(CloseableReference<CloseableImage> image) {
    Preconditions.checkState(CloseableReference.isValid(image));
    CloseableImage closeableImage = image.get();
    if (closeableImage instanceof CloseableStaticBitmap) {
        CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) closeableImage;
        return new BitmapDrawable(mResources, closeableStaticBitmap.getUnderlyingBitmap());
    } else {
        throw new UnsupportedOperationException("Unrecognized image class: " + closeableImage);
    }
}

nikslushkin avatar May 13 '15 13:05 nikslushkin

Hi @nikslushkin - I'm just wondering why you have the setImageFromCache code. Drawee will automatically look in the cache first, so you should just remove the line beginning boolean imageSet... and remove the if (!imageSet), and then it will work again for you.

IanChilds avatar May 13 '15 13:05 IanChilds

@IanChilds I also found this problem. I wanted to get bitmap and put it into ImageView(which I overwrite has zoom function). the following code:

public void setImageView(final ImageView imageView, Uri uri, int width, int height){ if(imageView == null){ return; } DataSubscriber dataSubscriber = new BaseDataSubscriber<CloseableReference>() { @Override public void onNewResultImpl( DataSource<CloseableReference> dataSource) { if (!dataSource.isFinished()) { return; } CloseableReference imageReference = dataSource.getResult(); if (imageReference != null) { try { // 可关闭的引用clone出来,不然在imageview使用过程中容易被回收 CloseableBitmap closeableBitmap = imageReference.clone().get(); Bitmap bitmap = closeableBitmap.getUnderlyingBitmap(); imageView.setImageBitmap(bitmap); } finally {
imageReference.close(); } } } @Override public void onFailureImpl(DataSource dataSource) { Throwable throwable = dataSource.getFailureCause(); // handle failure } }; getBitmap(imageView.getContext(), uri, width, height, dataSubscriber); }

public void getBitmap(Context context, Uri uri, int width, int height, DataSubscriber dataSubscriber){
    ImagePipeline imagePipeline = Fresco.getImagePipeline();
    ImageRequestBuilder builder = ImageRequestBuilder.newBuilderWithSource(uri);
    if(width > 0 && height > 0){
        builder.setResizeOptions(new ResizeOptions(width, height));
    }
    ImageRequest request = builder.build();
    DataSource<CloseableReference<CloseableImage>>
            dataSource = imagePipeline.fetchDecodedImage(request, context);
    dataSource.subscribe(dataSubscriber, UiThreadExecutorService.getInstance());
}

binqiangsun avatar Oct 08 '15 06:10 binqiangsun

@binqiangsun I am also getting same exception .. Did you get any workaround ? its helpful for me...Thanks

thizisarun avatar Oct 30 '15 12:10 thizisarun

@karun2189 I replaced ZoomImageView with ZoomableDraweeView. as followed: https://github.com/facebook/fresco/tree/master/samples/zoomable/src/main/java/com/facebook/samples/zoomable

binqiangsun avatar Nov 07 '15 02:11 binqiangsun