java - 如何在 Android 中打开具有 Uri 的文件?

标签 java android image file

我正在尝试打开存储在外部存储器上的图像。这是我的代码:

File imagePath = new File(imageURI);
InputStream inputStream=null;
try {
    inputStream = getContentResolver().openInputStream(Uri.parse(imageURI));
}catch(FileNotFoundException e){
    e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] originalImage = baos.toByteArray();

但它似乎无法找到该文件。 Uri 的格式为 content://com.android.providers.media.documents/document/image%3A21。 感谢您的帮助。

最佳答案

在我现在正在进行的一个项目中,我在 SD 卡上的目录中有外部图像。我正在使用

String thePath = Environment.getExternalStorageDirectory() + "/myAppFiles”;
        File imgFile = new File(thePath + " / " + "
externalImage.jpg ");
        if (imgFile.exists()) {
            try {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 6;
                Bitmap bm = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);

            }
        }

关于java - 如何在 Android 中打开具有 Uri 的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28928204/

相关文章:

java - 在 GAE 上使用 Hamcrest 1.3 和 Maven 的 NoSuchMethodError

android - API 29 媒体存储访问

java - 取消长轮询循环的并发问题

javascript - 添加指向数组中图像的链接

java - 如何验证 Wifi AP 或 Cell 是否有互联网?

java - 如何修复 Java 中的 Ldap 连接错误 "javax.naming.SizeLimitExceededException: [LDAP: error code 4 - Sizelimit Exceeded]"?

java - 添加和显示列表对象

java - 无法在android中使用httpconnection进行连接

html - .container > .wrapper > img : maximize img, 但不大于 .container(祖 parent ) - 怎么样?

javascript - 向我的图片库添加一个功能,允许图片在新窗口中打开