Intent.ACTION_VIEW打开文件报错处理

作者:陆金龙    发表时间:2018-07-10 08:04   


    Intent intent = new Intent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setAction(Intent.ACTION_VIEW);
    String type = getMIMEType(file);
    
    // intent.setDataAndType(QhFileProviderUtils.GetUriForFileByFileProvider(context, file), type);
    // 某些应用无法理解一个指向文件的content://格式的URI。以上方式无法加载文件。报错文件不存在或已损坏

    intent.setDataAndType(Uri.fromFile(file), type);
    // 使用Uri.fromFile(file) android N报错 android.os.FileUriExposedException: file:///storage/emulated/0/heqia/Chat/xxx.doc exposed beyond app through Intent.getData()
    // Application.onCreate中添加以下代码,设置一个不设防的VmPolicy,解决以上报错
    /**
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
       StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
       StrictMode.setVmPolicy(builder.build());
     }
     */
     context.startActivity(intent);