2016-08-03 48 views
2

我试图从SimpleDraweeView获取位图。从壁画获取位图

我已经设置好的我SimpleDraweeView与URI图像:

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

而现在我想将图像保存在手机的内存(存储)。

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

但是,如果使用上面的代码我得到:com.facebook.drawee.generic.RootDrawable不能转换到android.graphics.drawable.BitmapDrawable

我转换profileImage.getDrawable为位图图像是这样的:empty image

我试图让正在由该SimpleDraweeView

+0

你为什么要这样做? Fresco已经为您管理所有的缓存等。 –

+0

@AlexanderOprisnik我能想到的一个很好的理由就是用于UI测试,它可以访问可绘制的位图,但要做到这一点,我们需要从一个普通的drawable开始,并且我不知道如何投射那个 –

+0

因为我想让用户看到一些图像,然后将其保存到存储中 –

回答

0

显示获取位图绘制不是因为壁画的底层实现可以在浮图改变一个好主意,位图re,这会破坏你的代码。

我想你真的只是想要原始编码的图像字节(这样你就不必重新编码图像)。 你可以得到的编码图像字节与

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

既然你已经取出之前的图像,就可以将其瞬间,你可以这样做

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(); 
} 

不要忘记关闭这些参考资料可以让你不会泄漏记忆。 你可以找到更多的信息here

0

我认为它可以帮助你。

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

也许这不是一个漂亮的解决方案,但对我来说工作很好。