Getting bitmapdrawable from fresco

烈酒焚心 提交于 2019-12-23 16:47:22

问题


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


回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/38754327/getting-bitmapdrawable-from-fresco

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!