Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to use a recycled bitmap #648

Closed
binqiangsun opened this issue Oct 8, 2015 · 3 comments
Closed

Trying to use a recycled bitmap #648

binqiangsun opened this issue Oct 8, 2015 · 3 comments

Comments

@binqiangsun
Copy link

@binqiangsun binqiangsun commented Oct 8, 2015

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());
}
@IanChilds

This comment has been minimized.

Copy link
Contributor

@IanChilds IanChilds commented Oct 8, 2015

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 IanChilds closed this Oct 8, 2015
@binqiangsun

This comment has been minimized.

Copy link
Author

@binqiangsun binqiangsun commented Oct 9, 2015

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

@scandeezy

This comment has been minimized.

Copy link

@scandeezy scandeezy commented Feb 4, 2016

@binqiangsun

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

Matrix is the one unsupported type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.