java - 如何在我的应用程序 Nougat 中打开 wps office?

标签 java android android-7.0-nougat wps

我尝试了下面的代码,它在 Marshmallow 上运行,但在 Nougat 上不起作用。任何人都可以帮助我如何解决这个问题......

我的内部存储中有一个 Excelfile...如果未安装 wps,我将在单击按钮时打开 excelfile 我被重定向到 Playstore 以安装 wps.. .

我收到如下错误:

android.os.FileUriExposedException: file:///storage/emulated/0/Test%20Distributor%20Report.xlsx exposed beyond app through Intent.getData()

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());

    }

}

最佳答案

如果您的 targetSdkVersion 是 24 或更高版本,我们必须使用 FileProvider 类来授予对特定文件或文件夹的访问权限,以便其他应用可以访问它们。我们创建自己的类继承 FileProvider 以确保我们的 FileProvider 不与导入依赖项中声明的 FileProvider 冲突,如 here 所述。 .

将 file://uri 替换为 content://uri 的步骤:

  • 添加一个扩展 FileProvider 的类

    public class GenericFileProvider extends FileProvider {
    
    }
    
  • 在 AndroidManifest.xml 标签下添加一个 FileProvider 标签。为 android:authorities 属性指定一个唯一的权限以避免冲突,导入的依赖项可能会指定 ${applicationId}.provider 和其他常用的权限。

    <
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    <application
        ...
        <provider
            android:name=".GenericFileProvider"
            android:authorities="${applicationId}.my.package.name.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
    </application>
</manifest>
  • 然后在 res 文件夹下的 xml 文件夹中创建一个 provider_paths.xml 文件。如果文件夹不存在,可能需要创建。 该文件的内容如下所示。它描述了我们希望共享对根文件夹 (path=".") 中名为 external_files 的外部存储的访问权限。
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>
  • 最后一步是更改下面的代码行

    使用下面提到的代码打开excel文件:

    File file = new File(Environment.getExternalStorageDirectory()+ "/filepath/" + filename);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file),"application/vnd.ms-excel");
    startActivity(intent);
    
  • 编辑:如果使用 Intent 让系统打开您的文件,您可能需要添加以下代码行:

    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    

希望这会有所帮助...:)

请引用,完整代码和解决方案已解释here.

如果您的 targetSdkVersion 为 24 或更高,you can not use file: Uri values in Intents on Android 7.0+ devices .

您的选择是:

  1. 将您的 targetSdkVersion 降低到 23 或更低,或者

  2. 将您的内容放在内部存储上,然后 use FileProvider使其有选择地可供其他应用使用

例如:

Intent i=new Intent(Intent.ACTION_VIEW, FileProvider.getUriForFile(this, AUTHORITY, f));

i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(i);

关于java - 如何在我的应用程序 Nougat 中打开 wps office?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47177528/

相关文章:

java - 收到的 Intent 破坏了 Activity

java - 过滤器将 ArrayList<object> 添加到 HashMap

java - 如何解决这个 java.sql.SQLException : Statement parameter <number> not set

java - 解析二进制数据

android - Android 7.0版本通话界面上方弹窗

Android API 24。在包 'minimalSize' 中找不到属性 'android' 的资源标识符

java - Websocket 连接上出现“Access-Control-Allow-Origin”错误

Android - Samsung Galaxy S4 重启问题

android - Gradle 模块和 git 子模块

安卓 : How to write camera intent in android nougat