我有一個應用程序使用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很多,所以現在我在這裏發佈。 我想我做得對,也許只是一些小事情是錯的...
任何幫助將不勝感激!
你可以把互聯網的權限在Android的xml文件 – Riskhan
@kTekkie我不認爲你理解這個問題的權利,對不起 – Makks129
@ Makks129你是否使用JSON獲取圖像,如果是,那麼你可以與我分享一個示例代碼或者是否有simillar我可以在github上分叉,我的要求是:在GridView中使用JSON的Universal Image Loader + ----不想爲圖像使用常量鏈接 – Sun