android.os.FileUriExposedException: file:///storage/emulated/0/myimage/15115

来源:互联网 发布:视频加马赛克软件 编辑:程序博客网 时间:2024/06/07 02:39

当遇到这个问题的时候处理方法有2种

第一

//取消严格模式  FileProviderif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();    StrictMode.setVmPolicy( builder.build() );}

第二种方法

1 在AndroidManifest.xml中添加如下代码

<provider    android:name="android.support.v4.content.FileProvider"    android:authorities="包名.provider"    android:exported="false"    android:grantUriPermissions="true">    <meta-data        android:name="android.support.FILE_PROVIDER_PATHS"        android:resource="@xml/provider_paths"/></provider>


注意: authorities:app的包名.fileProvider grantUriPermissions:必须是true,表示授予 URI 临时访问权限 exported:必须是false resource:中的@xml/file_paths是我们接下来要添加的文件


2、在res目录下新建一个xml文件夹,并且新建一个file_paths的xml文件(如下图)



3、打开file_paths.xml文件添加如下内容


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


4 修改代码



                    Uri imageUri = Uri.fromFile(file);//原来的                    Uri imageUri = FileProvider.getUriForFile(MainActivity.this, MainActivity.this.getApplicationContext().getPackageName() + ".provider", file);

如果你的方法里面传入的context那么可以直接用上下文即可

如下


public static void installApkFile(Context context, String filePath) {        Intent intent = new Intent(Intent.ACTION_VIEW);        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);            Uri contentUri = FileProvider.getUriForFile(context, "com.yuneec.android.saleelfin.fileprovider", new File(filePath));            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");



阅读全文
0 0
原创粉丝点击