使用Android 7的FileUriExposedException

时间:2017-06-29 09:17:42

标签: android android-7.0-nougat

当我尝试捕捉图片时出现此错误:

  

致命的例外:主要                                                                      android.os.FileUriExposedException:   file:///storage/emulated/0/fname_1498727381241.jpg暴露在app之外   通过ClipData.Item.getUri()                                                                          在android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)                                                                          在android.net.Uri.checkFileUriExposed(Uri.java:2346)                                                                          在android.content.ClipData.prepareToLeaveProcess(ClipData.java:835)                                                                          在android.content.Intent.prepareToLeaveProcess(Intent.java:9514)                                                                          在android.content.Intent.prepareToLeaveProcess(Intent.java:9499)                                                                          在   android.app.Instrumentation.execStartActivity(Instrumentation.java:1525)                                                                          在android.app.Activity.startActivityForResult(Activity.java:4403)                                                                          在android.app.Activity.startActivityForResult(Activity.java:4362)                                                                          在   opteamit.com.belami.CommuniquerPartagerPhotosActivity $ 1.onClick(CommuniquerPartagerPhotosActivity.java:46)                                                                          在android.view.View.performClick(View.java:6261)                                                                          在android.view.View $ PerformClick.run(View.java:23752)                                                                          在android.os.Handler.handleCallback(Handler.java:751)                                                                          在android.os.Handler.dispatchMessage(Handler.java:95)                                                                          在android.os.Looper.loop(Looper.java:154)                                                                          在android.app.ActivityThread.main(ActivityThread.java:6776)                                                                          at java.lang.reflect.Method.invoke(Native Method)                                                                          在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1496)                                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)

之前有效,但似乎问题在于我使用的是Android 7(API 24)。

这是我的代码:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "fname_" +
                        String.valueOf(System.currentTimeMillis()) + ".jpg"));
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

2 个答案:

答案 0 :(得分:18)

嗯,正确的方法是使用FileProvideras mentioned in developer)。但我已经想出了一个顽皮的方法来做到这一点而不添加FileProvider。这显然是一种解决方法,但它确实有效。

在您的活动中,添加以下行:

 StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
 StrictMode.setVmPolicy(builder.build());

这将忽略URI暴露,您将获得访问权限。

是的,我知道这不是最好的做法。但我只想提供一个替代方案。

不过,推荐的方法是使用FileProvider

答案 1 :(得分:6)

  

如果您的targetSdkVersion为24或更高,我们必须使用FileProvider   用于访问特定文件或文件夹以进行访问的类   其他应用可访问。我们创建自己的类继承   FileProvider是为了确保我们的FileProvider不会发生冲突   如上所述,在导入的依赖项中声明了FileProviders   这里。

查找相关讨论here

从Android 7开始,我们不使用file:scheme作为意图的uri,你必须使用FileProvider。