如何从Uri获取位图对象(如果我成功地将其存储在/data/data/MYFOLDER/myimage.png
或file///data/data/MYFOLDER/myimage.png
中)以便在我的应用程序中使用它?
有没有人知道如何做到这一点?
发布于 2011-01-18 04:48:15
下面是正确的方法:
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);
}
}
如果您需要加载非常大的图像,下面的代码将以平铺方式加载它(避免大量内存分配):
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(myStream, false);
Bitmap region = decoder.decodeRegion(new Rect(10, 10, 50, 50), null);
查看答案here
发布于 2013-09-13 19:24:14
try
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(c.getContentResolver() , Uri.parse(paths));
}
catch (Exception e)
{
//handle exception
}
yes路径的格式必须如下所示
file:///mnt/sdcard/filename.jpg
发布于 2019-09-19 17:53:08
在API 29
中,MediaStore.Images.Media.getBitmap
似乎已被弃用。推荐的方式是使用API 28
中添加的ImageDecoder.createSource
。
下面是获取位图的方法:
val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.decodeBitmap(ImageDecoder.createSource(requireContext().contentResolver, imageUri))
} else {
MediaStore.Images.Media.getBitmap(requireContext().contentResolver, imageUri)
}
重要提示:ImageDecoder.decodeBitmap
读取EXIF方向,而Media.getBitmap
不读取
https://stackoverflow.com/questions/3879992
复制相似问题