Android N安装apk报错:android.os.FileUriExposedException
2017-03-11 22:59
477 查看
StackOverflow: http://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed
1.在AndroidMainifest.xml文件中添加:
###:
authorities:app的包名.fileProvider
grantUriPermissions:true,表示授予 URI 临时访问权限
exported:false
resource:下一步添加的文件
2.在res下新建一个名为xml的文件家,再新建文件provider_paths:
###:
path:需要临时授权访问的路径(.代表所有路径)
name:给访问路径命名
3.安装apk文件:
1.在AndroidMainifest.xml文件中添加:
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.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:下一步添加的文件
2.在res下新建一个名为xml的文件家,再新建文件provider_paths:
<?xml version="1.0" encoding="utf-8"?> <paths> <external-path name="files_root" path="Android/data/com.example.downloadapk/"/> <external-path name="external_storage_root" path="."/> </paths>
###:
path:需要临时授权访问的路径(.代表所有路径)
name:给访问路径命名
3.安装apk文件:
/** * 安装apk文件 * * @param apkFile 安装包所在目录 */ private void installApk(File apkFile) { Intent intent = new Intent(Intent.ACTION_VIEW); try { String[] command = {"chmod", "777", apkFile.toString()}; ProcessBuilder builder = new ProcessBuilder(command); builder.start(); } catch (IOException ignored) { } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".fileProvider", apkFile); intent.setDataAndType(contentUri, "application/vnd.android.package-archive"); } else { intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } startActivity(intent); }
相关文章推荐
- (转)Android N安装apk报错:android.os.FileUriExposedException
- 下载APK后打开APK,在安装时的报错 android.os.FileUriExposedException:exposed beyond app through Intent.getData()
- 开发中碰到的问题:Android 7.0 更新APK在安装时的报错 android.os.FileUriExposedException: file:///storage/emulated/0/And
- Android 7.0 更新APK在安装时的报错 android.os.FileUriExposedException(已修复)
- 解决 Android N 上 安装Apk时报错:android.os.FileUriExposedException: file:///storage/emulated/0/Download/appN
- Android 7.0 安装应用报错:android.os.FileUriExposedException
- Android 7.0调用系统相机报错Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/rpms/14
- Android7.0 android.os.FileUriExposedException
- 解决Android7.0相机 android.os.FileUriExposedException
- 解决 Android N 7.0 上 报错:android.os.FileUriExposedException
- 【适配整理】Android 7.0 调取系统相机崩溃解决android.os.FileUriExposedException
- 解决android7.0报错:android.os.FileUriExposedException
- android-解决 Android N 上 报错:android.os.FileUriExposedException
- 解决 Android N 上报错:android.os.FileUriExposedException: file:///storage/emulated/0/
- android.os.FileUriExposedException: file:///storage/emulated/0/ehcda exposed beyond app through...
- 解决 Android N 上报错:android.os.FileUriExposedException: file:///storage/emulated/0/
- android.os.FileUriExposedException:file:///storage/emulated/0/1502852028434.jpg exposed beyond app t
- 安卓7.0遇到 android.os.FileUriExposedException: file:///storage/emulated.. exposed beyond app through Intent.getData()
- Android 7.0 调取系统相机崩溃解决android.os.FileUriExposedException
- android.os.FileUriExposedException: file:///sdcard/update/wiiwork.apk exposed beyond app through I