Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

10.5. android.os.FileUriExposedException

下载 apk 文件,抛出 android.os.FileUriExposedException 异常

		
android.os.FileUriExposedException: file:///storage/emulated/0/Download/netkiller.apk exposed beyond app through Intent.getData()
		
		
		
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
		
		

此种方法不行

		
            intent.setDataAndType(Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive");		
		
		

改用 FileProvider.getUriForFile

		
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

            Uri contentUri = FileProvider.getUriForFile(context, "cn.netkiller.student.fileprovider", new File(filePath));
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
            intent.setPackage(context.getPackageName());
            context.startActivity(intent);