Wilson

自强不息 厚德载物

App共享文件Uri不能为file://

  • 先看异常信息:
E/StrictMode: null
              java.lang.Throwable: file:// Uri exposed through Intent.getData()
                  at android.os.StrictMode.onFileUriExposed(StrictMode.java:1757)
                  at android.net.Uri.checkFileUriExposed(Uri.java:2346)
                  at android.content.Intent.prepareToLeaveProcess(Intent.java:8045)
                  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1506)
                  at android.app.Activity.startActivityForResult(Activity.java:3930)
                  at android.app.Activity.startActivityForResult(Activity.java:3890)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:843)
                  at android.app.Activity.startActivity(Activity.java:4213)
                  at android.support.v4.app.ActivityCompatJB.startActivity(ActivityCompatJB.java:26)
                  at android.support.v4.app.ActivityCompat.startActivity(ActivityCompat.java:133)
                  at com.horizon.offer.mail.maildetail.impl.ImageAnnexWrapper.openFile(ImageAnnexWrapper.java:26)
                  at com.horizon.offer.mail.maildetail.MailDetailActivity.openAnnexFile(MailDetailActivity.java:184)
                  at com.horizon.offer.mail.maildetail.adapter.MailAnnexAdapter$MailAnnexViewHolder$1.onClick(MailAnnexAdapter.java:75)
                  at android.view.View.performClick(View.java:5204)
                  at android.view.View$PerformClick.run(View.java:21153)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

在StrictMode(严格)模式下,App之间共享资源报的异常。

  • 出现这个异常的代码
public void openFile(@NonNull Activity activity, @NonNull File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        /**异常这行代码**/
        intent.setDataAndType(Uri.fromFile(file), "image/*");
        ActivityCompat.startActivity(activity, intent, null);
    }

打印

Uri.fromFile(file)

输出信息

Uri uri=file:///storage/emulated/0/download/9text.jpg

Comments