我正在嘗試從我的應用程序在 Android 上打開一個 word 文檔(.doc 和 .docx),但我從 MS Word 應用程序中收到一個彈出錯誤:無法打開文件 | 嘗試將文件保存在設備上,然后打開它和 Google 文檔應用程序:無法打開文檔 | 我們無法打開您的文件該文檔是本地的,并與資產文件夾中的應用程序捆綁在一起,并且選擇器打開時不會發現任何錯誤,直到第三方應用程序嘗試打開該文檔。我相當肯定文件 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 Docs 中打開文件現在會在菜單欄中顯示文件名,而之前它是無標題的。來自 Google Docs 的新錯誤消息:無法打開文檔 | 此文件無法離線使用。在文件的選項菜單中使文件“脫機可用”。
添加回答
舉報
0/150
提交
取消