Android: Opening a Word document using intents and FileProvider

只愿长相守 提交于 2021-01-29 07:29:21

问题


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

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.

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

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>
...

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

Just for reference, my Uri looks like this:

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

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.


回答1:


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

Thank you all for your input.



来源:https://stackoverflow.com/questions/54836448/android-opening-a-word-document-using-intents-and-fileprovider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!