首页 文章

如何在我的应用程序Nougat中打开wps办公室?

提问于
浏览
1

我尝试下面的代码它正在使用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 回答

  • 1

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

    用content:// uri替换file:// 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);
    
    • Edit: 如果使用意图使系统打开您的文件,您可能需要添加以下代码行:
    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 .

    你的选择是:

    • targetSdkVersion 降至23或更低,或

    • 将您的内容放在内部存储上,然后use FileProvider将其有选择地提供给其他应用程序

    例如:

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

相关问题