简体   繁体   English

如何在Android中打开装有Uri的文件?

[英]How do I open a file I have the Uri for in Android?

I am trying to open an image I have stored on external memory. 我正在尝试打开我存储在外部存储器中的图像。 Here is the code I have: 这是我的代码:

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

But it doesn't seem to be able to locate the file. 但是它似乎无法找到该文件。 The Uri is in the format content://com.android.providers.media.documents/document/image%3A21. Uri的格式为content://com.android.providers.media.documents/document/image%3A21。 Thanks for any help. 谢谢你的帮助。

In a project I am working on now I have external images in a directory on the SD card. 现在在一个项目中,我在SD卡上的目录中有外部图像。 I am using 我在用

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

Try this: 尝试这个:

File imagePath = new File(imageURI.getPath());

url.getPath() returns a String in the following format: "/mnt/sdcard/xxx.jpg", without the scheme type pre-fixed url.getPath()返回以下格式的字符串:“ / mnt / sdcard / xxx.jpg”,而方案类型未预先固定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM