问题
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.
From 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>
From res/xml/files.xml
:
<paths>
<files-path name="internal_logs" path="logs/"/>
<files-path name="shared_files" path="shared/"/>
</paths>
From the Fragment
where I attempt to open the file:
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();
}
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
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.
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.
Any ideas?
回答1:
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.
回答2:
There is a good example here: https://developer.android.com/reference/android/support/v4/content/FileProvider.html
来源:https://stackoverflow.com/questions/24496847/sharing-images-from-internal-storage-using-fileprovider