6

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

我也想(的WhatsApp,脸谱,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")); 

photoUri在缓存文件夹中的URI其他应用程式没有阅读权限。

所以我用Google搜索/ stackoverflowed和found out,我需要使用FileProvider。

我配置FileProvider如在我的清单如下(as was written here):

<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> 

而file_paths.xml:

<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
    <cache-path name="photo_cache" path="/"/> 
</paths> 

然后我用在我的按钮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")); 

但是,根据应用程序我越来越奇怪除了蒸发散。就像这样:

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) 

还是我刚才看到的日志是这样的:

591-963/system_process W/ActivityManager﹕ Permission denied: checkComponentPermission() 

当然和应用程序本身给我祝酒有错误(他们不能打开或下载文件)。

我试过了,Google搜索,stackoverflowed很多,所以现在我在这里发布。 我想我做得对,也许只是一些小事情是错的...

任何帮助将不胜感激!

+0

你可以把互联网的权限在Android的xml文件 – Riskhan

+0

@kTekkie我不认为你理解这个问题的权利,对不起 – Makks129

+0

@ Makks129你是否使用JSON获取图像,如果是,那么你可以与我分享一个示例代码或者是否有simillar我可以在github上分叉,我的要求是:在GridView中使用JSON的Universal Image Loader + ----不想为图像使用常量链接 – Sun

回答

0

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

我一直在尝试使用此支持来共享其他应用程序的图像。它可以正常运行,包括图库,云端硬盘,Gmail和Keep。它无法使用照片和Google+。

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

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

+0

您是否在Instagram,Facebook或Messenger上试用过这种方法?它应该工作,但它并不是如此。 –