我想使用Google Androidify应用中显示的相同选项创建图片共享意图:
我使用Intent.ACTION_SEND
和Intent.ACTION_ATTACH_DATA
进行了测试,并且分别获得了相同的结果,我希望它们一起显示,为此我尝试使用Intent.EXTRA_INITIAL_INTENTS
:
Intent sendIntent = new Intent(Intent.ACTION_SEND);
Intent attachIntent = new Intent(Intent.ACTION_ATTACH_DATA);
sendIntent.setDataAndType(getTempImageUri(), "image/jpg");
attachIntent.setDataAndType(getTempImageUri(), "image/jpg");
Intent chooserIntent = Intent.createChooser(
attachIntent, getString(R.string.share) + ":");
chooserIntent.putExtra(
Intent.EXTRA_INITIAL_INTENTS, new Intent[] { sendIntent });
startActivity(chooserIntent);
但第二个Intent
结果显示为Android System
:
显然,Androidify应用使用的方法是Intent.ACTION_SEND
和Intent.ACTION_ATTACH_DATA
的混合。 任何人都知道怎么做?
感谢您的帮助!
答案 0 :(得分:4)
最后我发现了如何做到这一点,感谢这个答案:
https://stackoverflow.com/a/11038348/710274
这是我的最终代码,我相信有人会发现它很有用:
PackageManager pm = getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
Intent attachIntent = new Intent(Intent.ACTION_ATTACH_DATA);
sendIntent.setDataAndType(getTempImageUri(), "image/jpeg");
attachIntent.setDataAndType(getTempImageUri(), "image/jpeg");
Intent openInChooser = Intent.createChooser(sendIntent, "Share with:");
List<ResolveInfo> resInfo = pm.queryIntentActivities(attachIntent, 0);
Intent[] extraIntents = new Intent[resInfo.size()];
for (int i = 0; i < resInfo.size(); i++) {
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_ATTACH_DATA);
intent.setDataAndType(getTempImageUri(), "image/jpeg");
extraIntents[i] = new Intent(intent);
}
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);