Android: 使用 intents 和 FileProvider 打开 Word 文档
Android: Opening a Word document using intents and FileProvider
我正在尝试从我的应用程序在 Android 上打开一个 word 文档(.doc 和 .docx),但我从 MS Word 应用程序收到一个弹出错误:
Can't open file | Try saving the file on the device and then opening it
和 Google 文档应用程序:
Unable to open the document | We were unable to open your file
该文档是本地的,与资产文件夹中的应用程序捆绑在一起,在第三方应用程序尝试打开该文档之前,选择器打开时不会发现任何错误。
我相当确定文件 Uri 或 FileProvider 实现存在问题,但我不确定问题出在哪里。
在我的 AndroidManifest 文件中,我有以下内容:
...
<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文档的代码如下:
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 是这样的:
content://com.authority/external_files/Word%20Document.docx
更新:我重新格式化了文件名以删除空格并添加了 FLAG_GRANT_READ_URI_PERMISSION
标志。打开文件时,我仍然从 MS word 中收到相同的错误,但是在 Google 文档中打开文件现在在菜单栏中显示文件名,而在它之前是无标题的。 Google 文档中有一条新的错误消息:
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
标志。
谢谢大家的意见。
我正在尝试从我的应用程序在 Android 上打开一个 word 文档(.doc 和 .docx),但我从 MS Word 应用程序收到一个弹出错误:
Can't open file | Try saving the file on the device and then opening it
和 Google 文档应用程序:
Unable to open the document | We were unable to open your file
该文档是本地的,与资产文件夹中的应用程序捆绑在一起,在第三方应用程序尝试打开该文档之前,选择器打开时不会发现任何错误。
我相当确定文件 Uri 或 FileProvider 实现存在问题,但我不确定问题出在哪里。
在我的 AndroidManifest 文件中,我有以下内容:
...
<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文档的代码如下:
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 是这样的:
content://com.authority/external_files/Word%20Document.docx
更新:我重新格式化了文件名以删除空格并添加了 FLAG_GRANT_READ_URI_PERMISSION
标志。打开文件时,我仍然从 MS word 中收到相同的错误,但是在 Google 文档中打开文件现在在菜单栏中显示文件名,而在它之前是无标题的。 Google 文档中有一条新的错误消息:
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
标志。
谢谢大家的意见。