如何从 Uri 中获取 Bitmap 对象(如果我成功将其存储在
/data/data/MYFOLDER/myimage.png
或 file///data/data/MYFOLDER/myimage.png
) 在我的应用程序中使用它?
有没有人知道如何做到这一点?
最佳答案
这是正确的做法:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri imageUri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
}
}
如果您需要加载非常大的图像,以下代码会将其加载到图 block 中(避免大量内存分配):
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(myStream, false);
Bitmap region = decoder.decodeRegion(new Rect(10, 10, 50, 50), null);
查看答案here
关于android - 如何从 Uri 获取位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3879992/