android – 尝试使用Fresco从uri获取位图

当我使用Fresco使用 ImagePipeline获取Bitmap时,不明白行为.当我调试我的代码时,它正在执行onNewResultImpl或onFailureImpl,当我运行应用程序不工作意味着它没有被调用onFailureImpl或onNewResultImpl(我在运行应用程序时使用Toast和Log检查它).我见过这个 SO Question and take ref from it from Fresco's doc.
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
    super.onActivityResult(requestCode,resultCode,data);
    if (resultCode == Activity.RESULT_OK) {
        switch (requestCode) {
            case ACTION_OPEN_galLERY:
                mImageCaptureUri = data.getData();
                if (mImageCaptureUri != null) {
                    commentImgView.setimageURI(mImageCaptureUri);//mImageCaptureUri is working fine
                    try {
                        imageRequest = ImageRequestBuilder
                                .newBuilderWithSource(mImageCaptureUri)
                                .setRequestPriority(Priority.HIGH)
                                .setLowestPermittedRequestLevel(ImageRequest.RequestLevel.FULL_FETCH)
                                .build();
                        dataSource = imagePipeline.fetchDecodedImage(imageRequest,CommentActivity.this);
                        dataSource.subscribe(new BaseBitmapDataSubscriber() {
                            @Override
                            protected void onNewResultImpl(@Nullable Bitmap bitmap) {
                                if (bitmap != null) {
                                    bmp = Bitmap.createBitmap(bitmap);
                                    Log.d("Bitmap ","after callback");
                                    Toast.makeText(CommentActivity.this,"has bitmap",Toast.LENGTH_SHORT).show();
                                } else {
                                    Log.d("Bitmap is null ","bitmap is null",Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
                                Log.d("Bitmap ","after callback failure");
                                Toast.makeText(CommentActivity.this,"Failure",Toast.LENGTH_SHORT).show();
                            }
                        },CallerThreadExecutor.getInstance());
                    } catch (Exception e){
                        e.printstacktrace();
                    } finally {
                        if (dataSource != null) {
                            dataSource.close();
                        }
                    }
                }
        }
    }
}

注意:我试图从jpg图像获取位图,而不是从任何动画gif图

解决方法

我已经删除了try并最终阻止并关闭了onNewResultImpl和onFailureImpl中的Datasource

代码

ImageRequest imageRequest = ImageRequestBuilder
                            .newBuilderWithSource(mImageCaptureUri)
                            .setAutoRotateEnabled(true)
                            .build();

ImagePipeline imagePipeline = Fresco.getimagePipeline();
final DataSource<CloseableReference<CloseableImage>>
                            dataSource = imagePipeline.fetchDecodedImage(imageRequest,this);

dataSource.subscribe(new BaseBitmapDataSubscriber() {

      @Override
      public void onNewResultImpl(@Nullable Bitmap bitmap) {
         if (dataSource.isFinished() && bitmap != null){
                  Log.d("Bitmap","has come");
                  bmp = Bitmap.createBitmap(bitmap);
                  dataSource.close();
         }
     }

     @Override
     public void onFailureImpl(DataSource dataSource) {
        if (dataSource != null) {
                dataSource.close();
        }
     }
 },CallerThreadExecutor.getInstance());

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...