首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从Uri获取位图?

如何从Uri获取位图?
EN

Stack Overflow用户
提问于 2010-10-07 16:43:42
回答 17查看 399K关注 0票数 245

如何从Uri获取位图对象(如果我成功地将其存储在/data/data/MYFOLDER/myimage.pngfile///data/data/MYFOLDER/myimage.png中)以便在我的应用程序中使用它?

有没有人知道如何做到这一点?

EN

回答 17

Stack Overflow用户

发布于 2011-01-18 04:48:15

下面是正确的方法:

代码语言:javascript
复制
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);
    }
}

如果您需要加载非常大的图像,下面的代码将以平铺方式加载它(避免大量内存分配):

代码语言:javascript
复制
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(myStream, false);  
Bitmap region = decoder.decodeRegion(new Rect(10, 10, 50, 50), null);

查看答案here

票数 611
EN

Stack Overflow用户

发布于 2013-09-13 19:24:14

代码语言:javascript
复制
try
{
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(c.getContentResolver() , Uri.parse(paths));
}
catch (Exception e) 
{
    //handle exception
}

yes路径的格式必须如下所示

file:///mnt/sdcard/filename.jpg

票数 48
EN

Stack Overflow用户

发布于 2019-09-19 17:53:08

API 29中,MediaStore.Images.Media.getBitmap似乎已被弃用。推荐的方式是使用API 28中添加的ImageDecoder.createSource

下面是获取位图的方法:

代码语言:javascript
复制
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不读取

票数 44
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3879992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档