1

I'm trying to get fresco cached image from a fragment after a button click event to share the image bitmap in whatsapp!! Unfortunately it's very slow, after click the share button 2, 3 times code works!! I'm looking for better solution !!

 ImageRequest imageRequest = ImageRequestBuilder
                            .newBuilderWithSource(Uri.parse("http://example.com/test,jpg"))// disck cached 
                            .setRequestPriority(com.facebook.imagepipeline.common.Priority.HIGH)
                            .setLowestPermittedRequestLevel(ImageRequest.RequestLevel.DISK_CACHE)
                            .build();

                    //  .setRequestPriority(Priority.HIGH)

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

                    try {
                        dataSource.subscribe(new BaseBitmapDataSubscriber() {
                            @Override
                            public void onNewResultImpl(@Nullable Bitmap bitmap) {
                                if (bitmap == null) {
                                    Log.d(TAG, "Bitmap data source returned success, but bitmap null.");
                                    return;
                                }

                                sharewithwhatsappBitmap(bitmap);
                                // The bitmap provided to this method is only guaranteed to be around
                                // for the lifespan of this method. The image pipeline frees the
                                // bitmap's memory after this method has completed.
                                //
                                // This is fine when passing the bitmap to a system process as
                                // Android automatically creates a copy.
                                //
                                // If you need to keep the bitmap around, look into using a
                                // BaseDataSubscriber instead of a BaseBitmapDataSubscriber.
                            }

                            @Override
                            public void onFailureImpl(DataSource dataSource) {
                                // No cleanup required here
                                Log.d(TAG, "Bitmap data source onFailureImpl");
                            }
                        }, CallerThreadExecutor.getInstance());
                    } finally {
                        if (dataSource != null) {
                            dataSource.close();
                        }
                    }

I'm using DraweeController in recyclerview, DraweeController is more optimized I guess !! but don't how to use it for getting one image with bitmap !!

 public void sharewithwhatsappBitmap(Bitmap bitmap) {
                String path = MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(),
                        bitmap, "Image Description", null);
                Uri bmpUri = Uri.parse(path);
                Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                //shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
                shareIntent.putExtra(Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=garbagebin.com.garbagebin");


                shareIntent.setType("image/*");

                //startActivity(Intent.createChooser(shareIntent, "Share Image"));

                shareIntent.setPackage("com.whatsapp");

                        try {
                            startActivity(shareIntent);
                        } catch (android.content.ActivityNotFoundException ex) {

                            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.whatsapp")));

                        }

            }

Log when first time it calls for bitmap:

 v/unknown:AbstractDraweeController: controller 2a7f5b0e null -> 15: initialize
  V/unknown:AbstractDraweeController: controller 2a7f5b0e 15: setHierarchy: com.facebook.drawee.generic.GenericDraweeHierarchy@20cc392f
LOG_TAG
  • 19,894
  • 12
  • 72
  • 105
  • This is the recommended way of getting the bitmap. How big is your image? what file type? I guess it's a huge image, which takes some time to load from disk, decode, ... – Alexander Oprisnik Dec 07 '16 at 10:33
  • It's a a JPEG image of size 150kb with spec 1150x782 !!! I'm failed to find the solution to get the drawable from recycler view displayed image item where I'm using draweecontroler !!!!! I'm also looking for the solution where I can load the cache in the fragment – LOG_TAG Dec 07 '16 at 10:41
  • OK this should be faster then. If you're displaying the bitmap with drawee and then use your code from above, we only load the bitmap once, keep it cached and then re-use it, so this should be really quick. How slow is it currently? Maybe the bitmap handling code (`sharewithwhatsappBitmap`) makes it slow? We don't suggest getting the bitmap from the drawable since the Draweee implementation might change or the bitmap might be different (e.g. scaled down, postprocessed, rounded...). You can take a look at `GenericDraweeHierarchy` to see how the drawable is created. – Alexander Oprisnik Dec 07 '16 at 11:02
  • onNewResultImpl call back is slow, second time works fine randomly !! I'm using this logic https://github.com/kaedea/fresco-sample-usage/tree/master/app/src/main/java/me/kaede/frescosample/RecyclerViewto recyclerview but accessing the cache in fragment rather-than in the adapter to directly get the drawable from SimpleDraweeView!! I will look at GenericDraweeHierarchy Thanks – LOG_TAG Dec 08 '16 at 12:02
  • I have to do anything with ' PooledByteBuffer ' http://stackoverflow.com/questions/38754327/getting-bitmapdrawable-from-fresco with above mentioned code ?? – LOG_TAG Dec 08 '16 at 13:43
  • You can get the bitmap from the drawable as in your stackoverflow question. However, this will internally do something very similar to your existing code to get the image. You can try using a `BaseBitmapDataSubscriber` as described in "I just want a bitmap…" @ http://frescolib.org/docs/datasources-datasubscribers.html – Alexander Oprisnik Dec 08 '16 at 15:40

0 Answers0