java - 无法使用 FileProvider 和外部 PDF 编辑器保存 PDF 文件
问题描述
我的应用程序的 Android/data/packagename 文件夹中有各种 PDF,需要能够编辑它们。通过例如 Adobe Reader 的打开工作没有任何问题,并且 FileProvider 工作至今。当我关闭 PDF 编辑器时,未保存更改的文件。我尝试了不同的 PDF 编辑器。不幸的是,到目前为止我还没有找到任何其他选择。非常感谢您的帮助!
清单中的提供者
<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>
Java 代码
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)方法将在“r”模式下仅调用一次。
解决方案是使用 Intent.ACTION_EDIT
Intent intent = new Intent(Intent.ACTION_EDIT);
因此,当“编辑器应用程序”完成其工作时,您将看到对 FileProvider.openFile的第二次调用
,模式为“rw”: ->您自己的/本地应用程序文件将被保存;-)
推荐阅读
- vb.net - 在文本框 VB.Net 中调用子
- gradle - Gradle 6 迁移 settings.gradle.kts
- javascript - javascript cookie 在 angularjs 应用程序中过期时不会被删除
- batch-file - 批处理文件以帮助重复性任务
- javascript - 使用 Sinon 模拟构造函数中设置的类属性
- spring-boot - Maven Swagger swagger-codegen-plugin 只生成模型和控制器
- sql-server - 通过 ODBC 链接服务器将 AS400 (V7R3M0) 与 SQL Server 2017 连接时出现问题
- c# - 没有注册名为“默认”的 IUserTwoFactorTokenProvider。问题是两 (2) 个 ASP.NET Core 身份注册中的 AddDefaultTokenProviders()
- vue.js - 我无法让我的 vue 组件发出工作
- scala - 如何拆分元组scala列表