为什么使用fresco数据源获取位图是空的

时间:2015-05-27 09:04:43

标签: android facebook fresco

为什么在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());

3 个答案:

答案 0 :(得分:14)

我已经做了一些修改以使代码工作正常,请考虑使用它。我也测试了它,它工作得很好。

// 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());

答案 1 :(得分:4)

编辑:您使用的是fetchEncodedImage而不是fetchDecodedImage。这意味着每个图像返回都没有底层位图。但是,如果你将其更改为fetchDecodedImage并仍然看到空位图,那将是因为我在下面写的内容。

请在此处查看源代码:https://github.com/facebook/fresco/blob/master/imagepipeline/src/main/java/com/facebook/imagepipeline/datasource/BaseBitmapDataSubscriber.java#L57-L61

并非所有返回的图像都是CloseableBitmap s,而且那些没有返回的基础位图的图像,因此此方法返回空位图。

答案 2 :(得分:3)

如果您需要位图,则应致电fetchDecodedImage,而不是fetchEncodedImage,