无法使用FileProvider和外部PDF编辑器保存PDF文件



我在我的应用程序的Android/data/packagename文件夹中有各种pdf,需要能够编辑它们。通过adobereader等打开文件没有任何问题,文件提供程序到目前为止工作正常。当我关闭PDF编辑器时,更改的文件不会保存。我尝试了不同的PDF编辑器。不幸的是,到目前为止我还没有找到其他的选择。非常感谢您的帮助!

Provider in Manifest

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

provider_paths.xml

<paths>
<external-path name="external_files" path="."/>
</paths>
<<p>Java代码/strong>
public void openPDF(SetupFile setup) {
File pdfFile = new File(this.getExternalFilesDir(null).getAbsolutePath() + "/setups", setup.getFileName());
if (pdfFile.exists()) {
try {
Uri uri = FileProvider.getUriForFile(this.getContext(), this.getContext().getPackageName() + ".provider", pdfFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
} catch (Exception e) {
Toast.makeText(fThis.getActivity(), getString(R.string.error_no_pdf_editor), Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getActivity(), getString(R.string.error_file_not_exists), Toast.LENGTH_LONG).show();
}
}

你正在请求'ACTION_VIEW'文件,因此执行你的意图的应用程序会认为它只能读取它。

从技术上讲,这意味着FileProvider.openFile(android.net.Uri,java.lang.String)方法将只被调用一次,并带有" "模式。

解决方案是使用Intent。ACTION_EDIT

Intent intent = new Intent(Intent.ACTION_EDIT);
因此,当"编辑器应用程序"完成它的工作时,你会看到对FileProvider的第二次调用。openFile with mode "rw";
->你自己的/本地应用文件将被保存;-)

最新更新