如何从SimpleDraweeView获取位图?

时间:2017-01-30 16:45:21

标签: android bitmap fresco

我想从Fresco lib的Bitmap获取SimpleDraweeView,并将其保存到SD卡。 SimpleDraweeViewImageView的子类,因此它应该支持getDrawable(),但调用此方法会抛出ClassCastException

BitmapDrawable bitmapDrawable = (BitmapDrawable) viewHolder.drawee.getDrawable();

ClassCastException: com.facebook.drawee.generic.RootDrawable cannot be cast to android.graphics.drawable.BitmapDrawable

这是什么意思呢?

1 个答案:

答案 0 :(得分:2)

莫里森提到的s1rius的回答帮助了我。这是我使用的代码。

ImageRequest downloadRequest = ImageRequest.fromUri(uri);

CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(downloadRequest, context);

if (ImagePipelineFactory.getInstance().getMainFileCache().hasKey(cacheKey)) {
     BinaryResource resource = ImagePipelineFactory.getInstance().getMainFileCache().getResource(cacheKey);
     File cacheFile = ((FileBinaryResource) resource).getFile();
     File savedImageFile = new File(Constants.APP_PATH_SAVED_QUOTES, "M1iS1" + "_" + currentPosition + ".jpg");
}

然后我将cacheFile复制到savedImageFile