文章目录
- 一、遇到问题
- 二、解决问题
- 三、分析问题
一、遇到问题
--------- beginning of crash
2022-12-27 20:18:15.010 14422-14422/com.lisi.evidence_box E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lisi.evidence_box, PID: 14422
android.os.FileUriExposedException: file:///storage/emulated/0/EasyPusher/2022_12_27_20_16_08-0.mp4 exposed beyond app through Intent.getData()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:2089)
at android.net.Uri.checkFileUriExposed(Uri.java:2388)
at android.content.Intent.prepareToLeaveProcess(Intent.java:10827)
at android.content.Intent.prepareToLeaveProcess(Intent.java:10777)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1712)
at android.app.Activity.startActivityForResult(Activity.java:5258)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.support.v4.app.ActivityCompat.startActivityForResult(ActivityCompat.java:152)
at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:793)
at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:907)
at android.support.v4.app.Fragment.startActivity(Fragment.java:1009)
at android.support.v4.app.Fragment.startActivity(Fragment.java:998)
at com.lisi.evidence_box.activitys.MediaFilesActivity$LocalFileFragment.onClick(MediaFilesActivity.java:230)
at android.view.View.performClick(View.java:7185)
at android.view.View.performClickInternal(View.java:7162)
at android.view.View.access$3500(View.java:819)
at android.view.View$PerformClick.run(View.java:27678)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7562)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
二、解决问题
AndroidManifest.xml
<application
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.lisi.evidence_box.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
/res/xml/provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
- 【原本】Java源码
if (path.endsWith(".mp4")) {
try {
File f = new File(path);
Uri uri = Uri.fromFile(f);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*");
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
- 【改成】Java源码
if (path.endsWith(".mp4")) {
try{
File f = new File(path);
Uri contentUri = FileProvider.getUriForFile(getContext(), "com.your.package.provider", f);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(contentUri, "video/*");
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
三、分析问题
Android N对访问文件权限收回,按照Android N的要求,若要在应用间共享文件,您应发送一项 content://URI,并授予 URI 临时访问权限。
觉得好,就一键三连呗(点赞+收藏+关注)