使用FileProvider共享缓存的图像

Mak*_*129 6 android sharing universal-image-loader android-fileprovider

我有一个使用Universal Image Loader从互联网下载照片的应用程序,将它们缓存到data/data/com.myapp/cache并在ImageViews中显示.

我还想在我的应用程序中添加共享(WhatsApp,Facebook,Instagram,Dropbox等),所以我尝试使用这段代码:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, photoUri);
intent.setType("image/jpeg");
startActivity(Intent.createChooser(intent, "Share image"));
Run Code Online (Sandbox Code Playgroud)

photoUri是缓存文件夹中的uri,其他应用程序无权阅读.

所以我google/stackoverflowed 发现我需要使用FileProvider.

我配置了FileProvider如下(如在这里写在我的清单):

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.myapp.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)

和file_paths.xml:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="photo_cache" path="/"/>
</paths>
Run Code Online (Sandbox Code Playgroud)

然后我在按钮onClickListener的活动中使用以下代码:

File photoFile = ImageLoader.getInstance().getDiscCache().get("HTTP LINK HERE"); 
// returns File of "/data/data/com.myapp/cache/-1301123243"
Uri photoUri = FileProvider.getUriForFile(MyActivity.this, "com.myapp.fileprovider", photoFile);
// returns Uri of "content://com.myapp.fileprovider/photo_cache/-1301123243"
photoUri = Uri.parse(photoUri.toString() + ".jpg");
// then I add .jpg to file name

// Create intent
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, photoUri);
intent.setType("image/jpeg");

// Grant permissions to all apps that can handle this intent
// thanks to this answer http://stackoverflow.com/a/18332000
List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
    String packageName = resolveInfo.activityInfo.packageName;
    grantUriPermission(packageName, photoUri, 
        Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}

// And start
startActivity(Intent.createChooser(intent, "Share image"));
Run Code Online (Sandbox Code Playgroud)

但是,根据应用程序我会得到奇怪的例外.像这样:

1974-1974/com.whatsapp W/Bundle? Key android.intent.extra.STREAM expected ArrayList but value was a android.net.Uri$HierarchicalUri.  The default value <null> was returned.
1974-1974/com.whatsapp W/Bundle? Attempt to cast generated internal exception:
java.lang.ClassCastException: android.net.Uri$HierarchicalUri cannot be cast to java.util.ArrayList
        at android.os.Bundle.getParcelableArrayList(Bundle.java:1223)
        at android.content.Intent.getParcelableArrayListExtra(Intent.java:4425)
        at com.whatsapp.ContactPicker.d(ContactPicker.java:320)
        at com.whatsapp.ContactPicker.onCreate(ContactPicker.java:306)
        at android.app.Activity.performCreate(Activity.java:5104)
Run Code Online (Sandbox Code Playgroud)

或者我只看到这样的日志:

591-963/system_process W/ActivityManager? Permission denied: checkComponentPermission()
Run Code Online (Sandbox Code Playgroud)

当然,应用程序本身也会给我带来错误的祝酒(他们无法打开或下载文件).

我试过,谷歌搜索,stackoverflowed A LOT,所以现在我发布在这里.我想我做得对,也许只是一些小事是错的......

任何帮助将不胜感激!

Ric*_*h T 0

我认为这可能是 FileProvider 基础设施或您正在共享的应用程序的问题。

我一直在尝试使用此支持与其他应用程序共享图像。它与图库、云端硬盘、Gmail 和 Keep 配合使用效果良好。照片和 Google+ 失败。

我在 LogCat 中看到类似以下的条目,这让我相信 Uri 正在从一个 Activity 传递到另一个 Activity。但是,Intent 中设置的(临时)权限正在丢失。

11-19 21:14:06.031:I/ActivityManager(433):显示 com.google.android.apps.plus/.phone.HostPhotoViewIntentActivity:+848ms