问题描述
我有一些文件存储在应用程序的内部存储中,我想在外部应用程序中打开这些文件(例如,将图像发送到Gallery进行查看.)我已经设置了FileProvider
来执行此操作.
I have some files stored in my application's internal storage that I would like to open in an external application (sending an image to the Gallery for viewing, for example.) I've set up a FileProvider
to do so.
来自AndroidManifest.xml
:
<application>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${packageName}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/files"/>
</provider>
</application>
来自res/xml/files.xml
:
<paths>
<files-path name="internal_logs" path="logs/"/>
<files-path name="shared_files" path="shared/"/>
</paths>
在尝试打开文件的Fragment
中:
File sharedFolderPath = new File(getActivity().getFilesDir(), "shared");
File toOpen = new File(sharedFolderPath.getAbsolutePath(), sharedFile.getFileName());
if (toOpen.exists()) {
Log.w("File exists: " + toOpen.getAbsolutePath());
Uri fileUri = FileProvider.getUriForFile(getActivity(), BuildConfig.PACKAGE_NAME, toOpen);
Log.w("Attempting to attach file " + fileUri.getEncodedPath() + " with mime type: " + URLConnection.guessContentTypeFromName(fileUri.toString()));
Intent viewFile = new Intent(Intent.ACTION_VIEW);
viewFile.setData(fileUri);
viewFile.setType(URLConnection.guessContentTypeFromName(fileUri.toString()));
viewFile.putExtra(Intent.EXTRA_STREAM, fileUri);
viewFile.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
viewFile.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(viewFile);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getActivity(), "Please install an appropriate application to open this file.", Toast.LENGTH_SHORT).show();
}
} else {
log.w("File does not exist: " + toOpen.getAbsolutePath());
Toast.makeText(getActivity(), "file not found.", Toast.LENGTH_SHORT).show();
}
从logcat输出(共享到Gallery应用程序):
From the logcat output (sharing to the Gallery app):
PACKAGE_NAME W/TAG: File exists: /data/data/PACKAGE_NAME/files/shared/IMG_20140506_141221.jpg
PACKAGE_NAME W/TAG: Attempting to attach file /shared_files/IMG_20140506_141221.jpg with mime type: image/jpeg
778-791/? I/PackageManager﹕ Action: "android.intent.action.VIEW"
778-791/? I/PackageManager﹕ Category: "android.intent.category.DEFAULT"
778-791/? I/PackageManager﹕ Type: "image/jpeg"
778-791/? I/PackageManager﹕ Adding preferred activity ComponentInfo{com.google.android.gallery3d/com.android.gallery3d.app.GalleryActivity} for user 0 :
778-788/? I/ActivityManager﹕ START u0 {act=android.intent.action.VIEW typ=image/jpeg flg=0x3000001 cmp=com.google.android.gallery3d/com.android.gallery3d.app.GalleryActivity (has extras)} from pid 6209
778-1872/? I/ActivityManager﹕ Start proc com.google.android.gallery3d for activity com.google.android.gallery3d/com.android.gallery3d.app.GalleryActivity: pid=10602 uid=10039 gids={50039, 3003, 1028, 1015}
10602-10602/? V/StateManager﹕ startState class com.android.gallery3d.app.AlbumSetPage
正在发生的事情是找到了文件,生成了一个URI,并且可以正确检测到mimetype.我从Android获得了正确的处理程序列表,当我选择"Gallery"时,启动了Gallery活动,但是它直接进入设备自己的Gallery-没有错误消息,也没有打开的图像.此行为似乎与其他图像查看器一致-例如G +照片.
What's happening is that the file is found, a URI is generated, and the mimetype is detected correctly; I get an accurate list of handlers from Android, and when I choose "Gallery", the Gallery activity is launched, but it goes straight to the device's own gallery - no error message, but also no open image. This behavior seems consistent with other image viewers - G+ Photos for example.
真正奇怪的是,如果我在声明了Intent ACTION_SEND的情况下执行相同的代码,则该文件将正确地附加到Gmail中的电子邮件中. (实际上,这就是我对应用程序的内部日志记录的处理方式.)因此,似乎不太可能错误地格式化内容URI.我最好的猜测是我使用了错误的Intent操作?但是我不知道有什么比ACTION_VIEW更好的方法.
The really strange thing is if I execute the same code with the Intent ACTION_SEND declared, the file correctly attaches to an email in Gmail. (In fact, this is what I do with my app's internal logging.) So it seems really unlikely that I'm formatting the content URI incorrectly. My best guess is that I'm using the wrong Intent action? But I have no idea what would be better than ACTION_VIEW.
有什么想法吗?
推荐答案
将setType()
更改为setDataAndType()
,并摆脱EXTRA_STREAM
. EXTRA_STREAM
用于ACTION_SEND
,而不是ACTION_VIEW
,因此图库不知道您要查看的内容是什么.
Change setType()
to setDataAndType()
, and get rid of EXTRA_STREAM
. EXTRA_STREAM
is used for ACTION_SEND
, not ACTION_VIEW
, and so the Gallery has no idea what it is that you are asking it to view.
这篇关于使用FileProvider共享来自内部存储的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!