如何从 Uri 获取位图? [英] How to get Bitmap from an Uri?

查看:32
本文介绍了如何从 Uri 获取位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to get a Bitmap object from an Uri (if I succeed to store it in /data/data/MYFOLDER/myimage.png or file///data/data/MYFOLDER/myimage.png) to use it in my application?

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

Does anyone have an idea on how to accomplish this?

推荐答案

正确的做法如下:

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

如果你需要加载非常大的图片,下面的代码会分块加载(避免大量内存分配):

If you need to load very large images, the following code will load it in in tiles (avoiding large memory allocations):

BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(myStream, false);  
Bitmap region = decoder.decodeRegion(new Rect(10, 10, 50, 50), null);

此处查看答案

这篇关于如何从 Uri 获取位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆