why use fresco datasource to get bitmap is empty
Asked Answered
A

3

5

Why is bitmap returned in onNewResultImpl null?

final ImageView imageView = (ImageView) findViewById (R.id.imageView);

ImageRequest request = ImageRequest.fromUri(pic_uri);

ImagePipeline imagePipeline = Fresco.getImagePipeline();
DataSource dataSource = imagePipeline.fetchEncodedImage(request, this);
CloseableReference<CloseableImage> imageReference = null;
dataSource.subscribe (new BaseBitmapDataSubscriber() {
    @Override
    protected void onNewResultImpl(Bitmap bitmap) {
        LogUtils._d("onNewResultImpl....");
        if(bitmap == null) {
            LogUtils._d("bitmap is null");
        }
        imageView.setImageBitmap(bitmap);
    }

    @Override
    protected void onFailureImpl(DataSource dataSource) {
        LogUtils._d("onFailureImpl....");
    }
}, CallerThreadExecutor.getInstance());
Aphorism answered 27/5, 2015 at 9:4 Comment(1)
are you getting any error?if so post your logcat.or else have you checked your datasource is that null or there is something in that?Capriccioso
K
14

I have made some modification to make things work in your code, consider using this. I have also tested it and it worked all fine.

// To get image using Fresco
ImageRequest imageRequest = ImageRequestBuilder
          .newBuilderWithSource( Uri.parse(getFeedItem(position).feedImageUrl.get(index)))
          .setProgressiveRenderingEnabled(true)
          .build();

ImagePipeline imagePipeline = Fresco.getImagePipeline();
DataSource<CloseableReference<CloseableImage>> dataSource = 
                              imagePipeline.fetchDecodedImage(imageRequest,mContext);

 dataSource.subscribe(new BaseBitmapDataSubscriber() {

     @Override
     public void onNewResultImpl(@Nullable Bitmap bitmap) {
         // You can use the bitmap in only limited ways
         // No need to do any cleanup.
     }

     @Override
     public void onFailureImpl(DataSource dataSource) {
         // No cleanup required here.
     }

 }, CallerThreadExecutor.getInstance());
Kirimia answered 9/6, 2015 at 6:36 Comment(0)
D
4

EDIT: You are using fetchEncodedImage rather than fetchDecodedImage. That means that every image returns will have no underlying bitmap. But if you change that to fetchDecodedImage and still see null Bitmaps, it will be because of what I have written about below.

See the source code here: https://github.com/facebook/fresco/blob/master/imagepipeline/src/main/java/com/facebook/imagepipeline/datasource/BaseBitmapDataSubscriber.java#L57-L61

Not all images that are returned are CloseableBitmaps, and those that are not do not have an underlying bitmap to return, so this method returns a null Bitmap.

Discontinuance answered 29/5, 2015 at 10:58 Comment(1)
hi Binnie, just like you said: closeableImageRef.get() returns instance of CloseableAnimatedImage, not CloseableBitmap, so the Bitmap returned is null. Is there a way to show this image?Endotoxin
P
3

You should call fetchDecodedImage, not fetchEncodedImage, if you need a Bitmap.

Prior answered 29/5, 2015 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.