Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why use fresco datasource to get bitmap is empty

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());
like image 330
mio4kon Avatar asked May 27 '15 09:05

mio4kon


2 Answers

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());
like image 101
uniruddh Avatar answered Oct 29 '22 07:10

uniruddh


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.

like image 4
Binnie Avatar answered Oct 29 '22 06:10

Binnie