Android:使用Intent和FileProvider打开Word文档 [英] Android: Opening a Word document using intents and FileProvider

查看:99
本文介绍了Android:使用Intent和FileProvider打开Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的应用程序在Android上打开Word文档(.doc和.docx),但是从MS Word应用程序中出现弹出错误:

I am trying to open a word document (.doc and .docx) on Android from my app but I am getting a popup errors from the MS Word app:

无法打开文件|尝试将文件保存在设备上,然后将其打开

Can't open file | Try saving the file on the device and then opening it

和Google文档应用程序:

and the Google Docs app:

无法打开文档|我们无法打开您的文件

Unable to open the document | We were unable to open your file

该文档是本地文档,并且与该应用程序捆绑在资产文件夹中,并且选择器将打开,而不会捕获任何错误,直到第三方应用程序尝试打开该文档为止.

The document is local and bundled with the app in the assets folder, and the chooser opens without catching any errors until the third party app tries to open the document.

我不确定文件Uri或FileProvider的实现存在问题,尽管我不确定在哪里.

I am fairly certain there is an issue with the file Uri or FileProvider implementation, though I'm uncertain where.

在我的AndroidManifest文件中,我具有以下内容:

In my AndroidManifest file I have the following:

...
<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.STORAGE" />
...
<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.authority"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths"/>
    </provider>
...

我用来打开word文档的代码如下:

The code I am using to open the word document is the following:

try {
    String filePath = "Word Document.docx";
    Intent intent = new Intent(Intent.ACTION_VIEW);
    File file = new File(Environment.getExternalStorageDirectory(), filePath);

    Uri uri = FileProvider.getUriForFile(getActivity(), "com.authority", file);
    intent.setData(uri);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    try {
        startActivity(Intent.createChooser(intent, "Open Word document"));
    } catch (Exception e) {
        Toast.makeText(getActivity(), "Error: No app found to open Word document.", Toast.LENGTH_SHORT).show();
    }
} catch (Throwable t) {
    Toast.makeText(getActivity(), "Unable to open", Toast.LENGTH_SHORT).show();
}

仅供参考,我的Uri看起来像这样:

Just for reference, my Uri looks like this:

content://com.authority/external_files/Word%20Document.docx

更新:我重新格式化了文件名以删除空格,并添加了 FLAG_GRANT_READ_URI_PERMISSION 标志.打开文件时,我仍然从MS Word中收到相同的错误,但是在Google文档中打开文件现在在菜单栏中显示了文件名,而之前未命名.Google文档中有一条新的错误消息:

Update: I reformatted the filename to remove the spaces and added the FLAG_GRANT_READ_URI_PERMISSION flag. I am still getting the same error from MS word when opening the file, however opening the file in Google Docs now shows the filename in the menu bar whereas before it was Untitled. There is a new error message from Google Docs:

无法打开文档|该文件无法离线使用.在文件的选项菜单中将文件设置为离线可用".

Unable to open the document | This file isn't available offline. Make file "Available offline" in the file's options menu.

推荐答案

问题最终是由于文件url不正确而缺少 FLAG_GRANT_READ_URI_PERMISSION 标志.

The issue ended up being with the File url being incorrect combined with the missing FLAG_GRANT_READ_URI_PERMISSION flag.

谢谢大家的投入.

这篇关于Android:使用Intent和FileProvider打开Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆