3

I'm trying to get the bitmap that from SimpleDraweeView.

I already setted my SimpleDraweeView with uri image:

final Uri uri = new Uri.Builder()
                    .scheme(UriUtil.LOCAL_RESOURCE_SCHEME)
                    .path(returnUri.getPath())
                    .build();
            profileImage.setImageURI(returnUri);

And now I'm trying to save the image in the memory (storage) of the phone.

 Bitmap b = ((BitmapDrawable) profileImage.getDrawable()).getBitmap(); //  ImageDiskHelper.drawableToBitmap(profileImage.getDrawable().get);
        try {
            ImageDiskHelper.saveToInternalStorage(b, "MyActualProfileImage");
        } catch(Exception c){ }

But If I use the code above I get : com.facebook.drawee.generic.RootDrawable cannot be cast to android.graphics.drawable.BitmapDrawable

I convert profileImage.getDrawable to bitmap the image is like this: empty image

I'm trying to get the bitmap which is being displayed by the SimpleDraweeView

Dex Sebas
  • 247
  • 3
  • 13
  • Why exactly do you want to do this? Fresco already manages all the caching etc. for you. – Alexander Oprisnik Aug 04 '16 at 08:59
  • @AlexanderOprisnik A good reason I can think of is for UI testing it's useful to have access to a bitmap drawable, but to do that we need to start with a regular drawable it seems, and I don't know how to cast that – Nick Cardoso Aug 09 '16 at 10:24
  • Because I want to the user see some image then save it into storage – Dex Sebas Aug 10 '16 at 00:35

2 Answers2

1

Getting the bitmap drawable is not a good idea since the underlying implementation of Fresco could change in the future, which would break your code.

I guess you really just want the raw encoded image bytes (so that you don't have to re-encode the image). You can get the encoded image bytes with

DataSource<CloseableReference<PooledByteBuffer>> dataSource = imagePipeline.fetchEncodedImage(imageRequest, callerContext);

Since you already fetched the image before, it should be available instantly and you can do something like

try {
  CloseableReference<PooledByteBuffer> encodedImage = dataSource.getResult();
  if (imageReference != null) {
    try {
      // Do something with the encoded image, but do not keep the reference to it!
      // The image may get recycled as soon as the reference gets closed below.
    } finally {
      CloseableReference.closeSafely(encodedImage);
    }
  } else {
    // cache miss
    ...
  }
} finally {
  dataSource.close();
}

Don't forget to close the references so that you don't leak memory. You can find more information here.

Alexander Oprisnik
  • 1,212
  • 9
  • 9
1

I think it can be help you.

SimpleDraweeView view = findView(R.id.pic);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
if (bitmap != null) {
    dominantColor = getDominantColor(bitmap);
}

Maybe it's not beautiful solution, but working well for me.

Dyvoker
  • 2,489
  • 1
  • 9
  • 8