fresco icon indicating copy to clipboard operation
fresco copied to clipboard

Trying to use a recycled bitmap

Open binqiangsun opened this issue 11 years ago • 3 comments

the problem: java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@4716d650 at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1084) at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:844) at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:490) at android.widget.ImageView.onDraw(ImageView.java:1037)

I wanted to get bitmap and put the bitmap into "ImageView"(which was overwrited into ZoomImageVIew). So I used DataSubscriber and CloseableReference. as following code:

public void setImageView(final ImageView imageView, Uri uri, int width, int height){
    if(imageView == null){
        return;
    }
    DataSubscriber dataSubscriber = new BaseDataSubscriber<CloseableReference<CloseableBitmap>>() {
        @Override
        public void onNewResultImpl(
                DataSource<CloseableReference<CloseableBitmap>> dataSource) {
            if (!dataSource.isFinished()) {
                return;
            }
            CloseableReference<CloseableBitmap> imageReference = dataSource.getResult();
            if (imageReference != null) {
                try {
                    CloseableBitmap closeableBitmap = imageReference.clone().get();
                    Bitmap bitmap  = closeableBitmap.getUnderlyingBitmap();
                    if(bitmap != null && !bitmap.isRecycled()) {
                        imageView.setImageBitmap(bitmap);
                    }else{
                        Bitmap failedBitmap = BitmapFactory.decodeResource(imageView.getResources(), R.drawable.icon_load_failed);
                        imageView.setImageBitmap(failedBitmap);
                    }
                } 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

This is because you're not retaining any references to the CloseableBitmap, so it gets released when it is still in the ImageView.

You really should use DraweeView to do this, otherwise the memory management is very complicated.

IanChilds avatar Oct 08 '15 10:10 IanChilds

Is ScaleType.MATRIX available to the DraweeView? I want to do scale function for the DraweeView by gesture. But matrix seems useless to the DraweeView? @lanChilds

binqiangsun avatar Oct 09 '15 03:10 binqiangsun

@binqiangsun

According to the documentation here: http://frescolib.org/docs/scaling.html#_

Matrix is the one unsupported type.

scandeezy avatar Feb 04 '16 18:02 scandeezy