美文网首页
onFileUriExposed

onFileUriExposed

作者: WilsonMing | 来源:发表于2016-06-17 23:42 被阅读2053次

    App在StrictMode(严格)模式下运行模式下报的异常

    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 Holder$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)
    

    定位异常方法

    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)
    

    Android官网对这个异常的解释

    传递软件包网域外的 file:// URI 可能给接收器留下无法访问的路径。因此,尝试传递 file:// URI 会触发 FileUriExposedException。分享私有文件内容的推荐方法是使用 FileProvider。

    意思就是从7.0起不能使用绝对Uri地址,使用FileProvider转换URL
    那就来看怎么使用FileProvider使用
    原来方法改为

    @Override
        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.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(activity, OFRConstants.Authorities.AUTHORITIES_FILEPROVIDER, file);
            String contentMIMEType = activity.getContentResolver().getType(contentUri);
            intent.setDataAndType(contentUri, contentMIMEType);
            try {
                ActivityCompat.startActivity(activity, intent, null);
            } catch (ActivityNotFoundException e) {
                ToastUtils.showToast(activity, R.string.not_found_open_tool);
            }
        }
    
    public static class Authorities {
            public static final String AUTHORITIES_FILEPROVIDER = "com.mysiga.offer.fileprovider";
        }
    

    在AndroidManifest.xml定义

    <provider
                android:name="android.support.v4.content.FileProvider"
                android:authorities="com.mysiga.offer.fileprovider"
                android:exported="false"
                android:grantUriPermissions="true">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/paths_file" />
            </provider>
    

    xml下的paths_file.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
        <external-path
            name="annex"
            path="mysiga/annex/" />
    </paths>
    

    就解决报错了。

    相关文章

      网友评论

          本文标题:onFileUriExposed

          本文链接:https://www.haomeiwen.com/subject/wtwvdttx.html