android - 与另一个应用程序共享我的应用程序目录中的 PDF 文件有效但打开它没有
问题描述
我正在使用文件提供程序来保存和检索由我的应用程序生成的 PDF 文件。我想与其他 PDF 应用程序共享和打开这些文件。共享有效,但开放无效。这是共享和打开 PDF 文件的代码:
分享:
private void shareTheFile(int position) {
try {
File docPath=new File(SavedPdfActivity.this.getFilesDir(),"Documents");
Log.d(TAG, "shareTheFile: document dir path="+docPath.getAbsolutePath());
File file = new File(mList.get(position).getFilePath());
Log.d(TAG, "shareTheFile: file path as saved in db="+file.getAbsolutePath());
String fileName=file.getName();
File newFile=new File(docPath, fileName);
Log.d(TAG, "shareTheFile: new file path="+newFile.getAbsolutePath());
Uri uri= FileProvider.getUriForFile(SavedPdfActivity.this, "com.rewoke.prixpi.pdfprovider", newFile);
Log.d(TAG, "shareTheFile: uri="+uri.toString());
Intent intentShareFile = new Intent();
intentShareFile.setAction(Intent.ACTION_SEND);
intentShareFile.setDataAndType(uri, getContentResolver().getType(uri));
intentShareFile.putExtra(Intent.EXTRA_STREAM, uri);
intentShareFile.putExtra(Intent.EXTRA_TEXT, mList.get(position).getFileName());
PackageManager pm = getPackageManager();
if (intentShareFile.resolveActivity(pm) != null) {
startActivity(Intent.createChooser(intentShareFile, "Share file"));
}
}catch (Exception e){
Log.d(TAG, "shareTheFile: error msg="+e.getMessage());
e.printStackTrace();
}
}
并打开 PDF:
private void openThePdf(int position) {
File docPath=new File(SavedPdfActivity.this.getFilesDir(),"Documents");
File file = new File(mList.get(position).getFilePath());
String fileName=file.getName();
File newFile=new File(docPath, fileName);
try {
Uri uri = FileProvider.getUriForFile(SavedPdfActivity.this, "com.application.name.pdfprovider", newFile);
Intent openIntent = new Intent(Intent.ACTION_VIEW, uri);
openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
PackageManager pm = getPackageManager();
if (openIntent.resolveActivity(pm) != null) {
startActivity(Intent.createChooser(openIntent, "Open file"));
}
}catch (Exception e){
Log.d(TAG, "openThePdf: error msg="+e.getMessage());
}
}
尝试使用 Drive PDF Viewer 应用程序打开文件时出现以下错误:
openFd: java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
我的文件提供者:
<provider
android:authorities="{$appliation_name}.pdfprovider"
android:name=".helper.CustomFileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths"/>
</provider>
我的日志:
2019-12-23 12:17:26.100 19822-19822/com.rewoke.prixpi D/SavedPdfActivity: shareTheFile: document dir path=/data/user/0/com.rewoke.prixpi/files/Documents
2019-12-23 12:17:26.100 19822-19822/com.rewoke.prixpi D/SavedPdfActivity: shareTheFile: file path as saved in db=/storage/emulated/0/Android/data/com.rewoke.prixpi/files/Documents/PrixpiPDF_2019-12-357 11:30:503867655244253323634.pdf
2019-12-23 12:17:26.100 19822-19822/com.rewoke.prixpi D/SavedPdfActivity: shareTheFile: new file path=/data/user/0/com.rewoke.prixpi/files/Documents/PrixpiPDF_2019-12-357 11:30:503867655244253323634.pdf
2019-12-23 12:17:26.101 19822-19822/com.rewoke.prixpi D/SavedPdfActivity: shareTheFile: uri=content://com.rewoke.prixpi.pdfprovider/my_docs/PrixpiPDF_2019-12-357%2011%3A30%3A503867655244253323634.pdf
解决方案
推荐阅读
- android-studio - 如何一次删除多行上的标签
- javascript - D3.js 高级节点连接(查找路径)
- php - 您的 PHP 安装似乎缺少 WordPress ubuntu 18.10 所需的 MySQL 扩展
- android - 带字符串的枚举或密封类
- node.js - 使用 firebase cloud 功能将图像保存到 gcloud
- linux - 用于检查 DNS 是否具有与同一列表中旁边的 IP 相同的 IP 的 Linux 脚本
- apache-kafka - Kafka - 无法列出新的消费者组
- python - USB端口号可以用作客户端服务器模型的端口吗?
- opacity - iText 背景不透明度
- python - 将 pandas 行中的值重新格式化为关联列