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());
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
mio4kon
  • 1,503
  • 1
  • 10
  • 15
  • 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? – rogerwar May 28 '15 at 16:10

3 Answers3

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());
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
Uniruddh
  • 4,427
  • 3
  • 52
  • 86
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.

Binnie
  • 51
  • 2
  • 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? – tainy Aug 11 '17 at 10:04
3

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

tyronen
  • 2,558
  • 1
  • 11
  • 15