使用FileProvider从内部存储共享图像

sig*_*eta 7 android android-contentprovider android-fileprovider

我有一些文件存储在我的应用程序的内部存储器中,我想在外部应用程序中打开(例如,将图像发送到图库以供查看).我已经设置了FileProvider这样做.

来自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>
Run Code Online (Sandbox Code Playgroud)

来自res/xml/files.xml:

<paths>
    <files-path name="internal_logs" path="logs/"/>
    <files-path name="shared_files" path="shared/"/>
</paths>
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

从logcat输出(共享到Gallery应用程序):

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
Run Code Online (Sandbox Code Playgroud)

发生的事情是找到文件,生成URI,并正确检测mimetype; 我从Android获得了准确的处理程序列表,当我选择"Gallery"时,Gallery活动已启动,但它直接进入设备自己的库 - 没有错误消息,但也没有打开的图像.此行为似乎与其他图像查看器一致 - 例如G + Photos.

真正奇怪的是,如果我使用声明的Intent ACTION_SEND执行相同的代码,则文件会正确附加到Gmail中的电子邮件中.(事实上​​,这就是我对应用程序的内部日志记录所做的事情.)所以我似乎不太可能错误地格式化内容URI.我最好的猜测是我使用了错误的Intent动作?但我不知道什么比ACTION_VIEW更好.

有任何想法吗?

Com*_*are 4

更改setType()setDataAndType(),并摆脱EXTRA_STREAMEXTRA_STREAM用于ACTION_SENDACTION_VIEW而不是 ,因此图库不知道您要求它查看什么。