wvd*_*vdz 1 android uri image filenotfoundexception
使用ACTION_GET_CONTENTIntent选择图像时,我得到一个无法从中打开文件的 URI。如果我尝试打开文件,如下所示:
InputStream in = new FileInputStream(new File(uri.getPath()));
Run Code Online (Sandbox Code Playgroud)
它给出了以下异常:
03-11 15:14:36.132 20557-20557/my.package W/System.err? java.io.FileNotFoundException: /document/image:9537: open failed: ENOENT (No such file or directory)
03-11 15:14:36.138 20557-20557/my.package W/System.err? at libcore.io.IoBridge.open(IoBridge.java:456)
03-11 15:14:36.138 20557-20557/my.package W/System.err? at java.io.FileInputStream.<init>(FileInputStream.java:76)
Run Code Online (Sandbox Code Playgroud)
/document/image:9537 似乎确实是一条不正确的路径,但我如何获得正确的路径?
我使用这个逻辑打开图像选择器:
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra("return-data", false);
startActivityForResult(Intent.createChooser(photoPickerIntent, "Complete action using"), PICK_FROM_FILE);
Run Code Online (Sandbox Code Playgroud)
并在 onActivityResult 中检索 Uri,如下所示:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
....
Uri uri = data.getData();
Run Code Online (Sandbox Code Playgroud)
我需要让文件进行解码以使其更小。
如果我尝试打开文件,如下所示:
这不适用于大多数现代 Android 设备。最有可能的是,您收到了一个content: Uri. 这在较新版本的 Android 上是相当正常的。未来版本的 Android 可能会阻止file: Uri值。
我需要让文件进行解码以使其更小。
不必有与给定Uri. 这Uri可能指向:
BLOB数据库列中的字节流使用ContentResolver和openInputStream()来获取InputStream指向的内容上的Uri。然后,将其传递给您的解码逻辑,例如BitmapFactory及其decodeStream()方法。