Try to fetch bitmap from uri using Fresco
Asked Answered
F

1

7

Don't understand the behavior while I am fetching Bitmap using Fresco using ImagePipeline. When I debug my code it is executing onNewResultImpl or onFailureImpl and when I run the application is not working means it is not getting called onFailureImpl or onNewResultImpl (I am checking it using Toast and Log while running the app). I have seen this SO Question and take ref from it and also 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 ","after callback");
                                    Toast.makeText(CommentActivity.this,"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();
                        }
                    }
                }
        }
    }
}

Note: I am trying to get bitmap from jpg image not from any animated gif image

Forth answered 5/10, 2015 at 13:32 Comment(6)
try declaring imageRequest, imagePipeline and dataSource to class levelDainedainty
make sure you are using latest library files and jar filesDainedainty
what values does data variable contain? make sure you are retriving correct valuesDainedainty
no I haven't. didn't need while picasso and glide are around. but had made some demo earlierDainedainty
there is a bit of problem using glide because it resize the bitmap as per imageview's height widthForth
need to check fresco out, will try making a demo for thisDainedainty
F
14

I have removed try and finally block and and closing the Datasource inside onNewResultImpl and onFailureImpl

code snippet

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());
Forth answered 12/10, 2015 at 10:29 Comment(2)
Wonder why you need Bitmap.createBitmap(bitmap) instead of simply bmp = bitmap?Virginavirginal
That is not full code I was scaling the bitmap there and assigned that to bmpForth

© 2022 - 2024 — McMap. All rights reserved.