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"));
photoUri は、他のアプリが読み取る権限を持たないキャッシュ フォルダー内の uri でした。
だから私はgoogled/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>
そして 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()
そしてもちろん、アプリ自体がエラーで乾杯します (ファイルを開いたりダウンロードしたりできません)。
私はたくさん試し、グーグルで調べ、スタックオーバーフローしたので、今ここに投稿しています。私はそれを正しくやっていると思います、多分何か小さなことが間違っているだけです...
どんな助けでも大歓迎です!