【问题标题】:Sharing images from internal storage using FileProvider使用 FileProvider 从内部存储共享图像
【发布时间】:2014-06-30 18:45:07
【问题描述】:

我有一些文件存储在我的应用程序的内部存储中,我想在外部应用程序中打开这些文件(例如,将图像发送到图库以供查看。)我已经设置了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>

来自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 应用程序):

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 获得了准确的处理程序列表,当我选择“画廊”时,画廊活动启动,但它直接进入设备自己的画廊 - 没有错误消息,也没有打开的图像。这种行为似乎与其他图像查看器一致 - 例如 G+ 照片。

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

有什么想法吗?

【问题讨论】:

    标签: android android-contentprovider android-fileprovider


    【解决方案1】:

    setType() 更改为setDataAndType(),并去掉EXTRA_STREAMEXTRA_STREAM 用于ACTION_SEND,而不是ACTION_VIEW,因此画廊不知道您要查看的是什么。

    【讨论】:

    • 我已经在没有EXTRA_STREAM 的情况下尝试过这个;我把它留在问题中是为了表达“我已经尝试过这个”,尽管我认为它不正确或没有必要。但是,使用setDataAndType() 似乎有效,而setData()setType() 没有。我原以为这只是将两者结合起来的一种方便方法,但这似乎是不正确的。你知道为什么这可能会在两个分开的时候不起作用吗?
    • @sigmabeta: setType() 消除了您在之前的 setData() 调用中设置的 Uri(或传递给构造函数)。我不知道为什么,这很烦人。因此,setType() 本身很少是正确的答案——ACTION_SEND 是为数不多的正确答案之一。
    • 是的,我现在在setType() 的javadoc 中看到了它——这是一种非常不直观的行为,尽管阅读了它,我现在看到了它存在的必要性。感谢您的帮助。
    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多