我尝试下面的代码它正在使用Marshmallow它不在Nougat工作..任何人都可以帮我解决这个问题...
我的内部存储空间中有 Excelfile ...如果wps未安装,点击按钮时会打开excelfile我被重定向到 Playstore 以安装wps ...
I am getting error like:
android.os.FileUriExposedException:file:///storage/emulated/0/Test%20Distributor%20Report.xlsx通过Intent.getData()暴露在app之外
void openExcel()
{
Intent intent = new Intent(Intent.ACTION_VIEW);
/*vnd.ms-excel*/ /*application/vnd.openxmlformats-officedocument.spreadsheetml.sheet*/
intent.setDataAndType(Uri.fromFile(file),"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
try {
context.startActivity(intent);
}catch (Exception e)
{
Toast.makeText(context,"Please Install WPS Application To View Reports",Toast.LENGTH_LONG).show();
Intent viewIntent =
new Intent("android.intent.action.VIEW",
Uri.parse("https://play.google.com/store/apps/details?id=cn.wps.moffice_eng&hl=en"));
context.startActivity(viewIntent);
Log.d("printexeption",""+e.toString());
}
}
1 回答
如果您的targetSdkVersion为24或更高,我们必须使用 FileProvider class来授予对特定文件或文件夹的访问权限,以使其可供其他应用程序访问 . 我们创建自己的类继承
FileProvider
,以确保我们的FileProvider不会与导入的依赖项中声明的FileProviders冲突,如here所述 .用content:// uri替换file:// uri的步骤:
android:authorities
属性指定唯一权限以避免冲突,导入的依赖项可能指定${applicationId}.provider
和其他常用权限 .res
文件夹下的xml
文件夹中创建provider_paths.xml
文件 . 如果文件夹不存在,则可能需要创建文件夹 . 该文件的内容如下所示 . 它描述了我们希望共享对根文件夹(path=".")
的外部存储的访问,名称为 external_files .使用下面提到的代码打开excel文件:
希望这会有所帮助...... :)
请参考,完整的代码和解决方案已被解释here.
如果
targetSdkVersion
为24或更高,you can not use file: Uri values in Intents on Android 7.0+ devices .你的选择是:
将
targetSdkVersion
降至23或更低,或将您的内容放在内部存储上,然后use FileProvider将其有选择地提供给其他应用程序
例如: