其他分享
首页 > 其他分享> > 使用Android 7的FileUriExposedException

使用Android 7的FileUriExposedException

作者:互联网

参见英文答案 > android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()                                    23个
当我尝试捕获图片时出现此错误:

FATAL EXCEPTION: main
android.os.FileUriExposedException:
file:///storage/emulated/0/fname_1498727381241.jpg exposed beyond app
through ClipData.Item.getUri()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
at android.net.Uri.checkFileUriExposed(Uri.java:2346)
at android.content.ClipData.prepareToLeaveProcess(ClipData.java:835)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9514)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9499)
at
android.app.Instrumentation.execStartActivity(Instrumentation.java:1525)
at android.app.Activity.startActivityForResult(Activity.java:4403)
at android.app.Activity.startActivityForResult(Activity.java:4362)
at
opteamit.com.belami.CommuniquerPartagerPhotosActivity$1.onClick(CommuniquerPartagerPhotosActivity.java:46)
at android.view.View.performClick(View.java:6261)
at android.view.View$PerformClick.run(View.java:23752)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at 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);

解决方法:

If your targetSdkVersion is 24 or higher, we have to use FileProvider
class to give access to the particular file or folder to make them
accessible for other apps. We create our own class inheriting
FileProvider in order to make sure our FileProvider doesn’t conflict
with FileProviders declared in imported dependencies as described
here.

查找相关讨论here

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

标签:android-7-0-nougat,android
来源: https://codeday.me/bug/20191006/1861494.html