我正在尝试打开存储在外部存储器上的图像。这是我的代码:
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/