我想将 .gif 从我的应用程序分享到 facebook、twitter 等。我可以将 .png 等简单图像分享到 facebook、twitter 但无法分享任何 .gif 图像。实际上,我想从 URL 共享 .gif 图像。因此,起初,我尝试从 sdcard 共享 .gif 图像并成功,但无法从 drawable 和 URL 成功。我怎样才能通过这两种方式共享 .gif 图像?以下代码成功从 sdcard 共享 .gif。
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "g1.gif");
Uri pngUri = Uri.fromFile(yourFile);
//Not working from URL
//Uri pngUri = Uri.parse("http://www.gamecastor.com/iphoneapps/emoji/gangnamemoji/Group1/g1.gif");
//Not working from drawable
//Uri pngUri = Uri.parse("android.resource://com.example.asd/drawable/g1.gif");
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(android.content.Intent.EXTRA_STREAM, pngUri);
//code to share to facebook only
PackageManager pm = getApplicationContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ((app.activityInfo.name).contains("facebook")) {
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName,activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
startActivity(shareIntent);
}
}