我创建了一个可以在其内部存储中导入文件的应用程序。为了使用外部应用程序(例如 PF 查看器或照片)打开文件,我尝试遵循以下指南:官方指南、topic1、topic2、topic3和topic4 ,但没有成功。
这是我的代码:
在我的清单中
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myapp.chatcher"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
我的包裹价值:package="com.myapp.catcher"
我的 file_paths.xml
<paths
xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="projection" path="." />
</paths>
我的代码
String fileName = path.substring(path.lastIndexOf("/") + 1);
String shelf = path.substring(path.lastIndexOf("PRIVATE") + 8, path.lastIndexOf("/"));
File filePath = new File(mContext.getFilesDir(), "PRIVATE".concat("/").concat(shelf).concat("/"));
File newFile = new File(filePath, fileName);
Uri contentUri = FileProvider.getUriForFile(mContext, "com.myapp.chatcher", newFile);
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(contentUri);
myIntent.setType(mimeType);
myIntent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION);
mContext.startActivity(myIntent);
我创建了这样的层次结构:
PRIVATE -> shelf1 -> my files
-> shelf2 -> my files
-> shelfN -> my files
例如:data/user/0/com.myapp.chatcher/files/PRIVATE/testshelf/Screenshot_2017-01-04-09-45-13.png
打印 newFile.getAbsolutePath() 的结果是
/data/user/0/com.myapp.chatcher/files/PRIVATE/bogl/imagetest.jpg
此代码打开选择器,我可以在其中单击“照片”,然后打开“照片应用程序”,但不显示 imagetest.jpg,而是显示所有图片所在的文件夹。如果我尝试使用 pdf 文件,它不会打开 pdf,并且会显示“无媒体”消息。
我的代码有什么问题?