![](/img/trans.png)
[英]Android: Opening a Word document using intents and FileProvider
[英]Android FileProvider and Intents - The changes to the file do not reflect on the actual file
我有一些 PDF 文件存儲在/storage/emulated/0/Name123
目錄中,例如:
/storage/emulated/0/Name123/Key1/randomName1.pdf
/storage/emulated/0/Name123/Key2/randomName2.pdf
/storage/emulated/0/Name123/Key3/randomName3.pdf
and so on
考慮到這些文件,我的 java android-studio-based 應用程序有一個創建ACTION_VIEW
Intent
的按鈕。 這個想法是允許用戶使用用戶選擇的 PDF 查看器/編輯器應用程序打開 PDF 文件。 一切都很好,除了一件事:對 PDF 文件的所有更改和編輯都不會反映駐留在文件系統上的實際文件。 因此,如果您單擊按鈕,打開 PDF 並突出顯示頁面上的一些行,保存它,然后關閉 PDF 閱讀器應用程序並返回我們的應用程序並單擊按鈕再次打開相同的文件,相同的 ZBCD1B68037759BDDFC1B6沒有所有這些更改將被打開。
我注意到Stackoverflow 上沒有任何答案的類似問題。
順便說一句,我不想使用像下面的代碼這樣的解決方案來避免在新版本的FileUriExposedException
上向Intent
提供file URI
引起的 FileUriExposedException:
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
有沒有其他方法可以啟動另一個獨立應用程序以在某個absolute path
中打開 PDF 文件,保留對該文件的寫訪問權限?
鑒於新 Android 版本上文件 URI 的新限制,文件管理器應用程序如何啟動第三方應用程序來打開文件,因為我確認我可以使用 X-plore 打開 PDF 文件,編輯它,然后將其保存回文件系統上的實際輸入文件具有相同的 PDF 閱讀器應用程序,這些應用程序無法獲得對我的內容 URI 的寫訪問權限,例如 Adobe Reader、Foxit Reader、XODO。
這是我的代碼(按照本主題使用 FileProvider) :
文件:清單
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<application
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
</manifest>
文件:xml/provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>
文件:MainActivity.java 按鈕的 onClickListener
String key = "Key2/randomName2.pdf"
File path = new File("/storage/emulated/0/Name123");
File file = new File(path, key);
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = FileProvider.getUriForFile(getApplicationContext(),getApplicationContext().getPackageName()+".provider", file);
intent.setDataAndType(uri, "application/pdf");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(intent, null);
// The application has the required permissions to access and manage ALL FILES on the external memory.
您要求“ACTION_VIEW”文件,因此尊重您意圖的應用程序將認為它只能讀取它。
解決方案是使用 Intent.ACTION_EDIT
Intent intent = new Intent(Intent.ACTION_EDIT);
我的答案中的更多詳細信息: 無法使用 FileProvider 和外部 PDF 編輯器保存 PDF 文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.