我正在我的应用程序中实现共享。使用 whatsapp、twitter 等应用程序可以共享图像+文本等混合内容,而 facebook、linkedin 等仅允许一种类型的内容(图像或文本)。如何做到这一点?Eg when whatsapp is selected it should send both, whereas when facebook is selected it should share text. 我想到的一种解决方案是查询所有可以执行此操作的活动,然后创建目标意图。以下是我迄今为止尝试过的
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, title);
shareIntent.putExtra(Intent.EXTRA_TEXT, title + "\n" + message);
List<ResolveInfo> resolveInfos = activity.getApplicationContext().getPackageManager().queryIntentActivities(shareIntent, PackageManager.MATCH_DEFAULT_ONLY);
ArrayList<Intent> mainIntentList = new ArrayList();
Iterator<ResolveInfo> it = resolveInfos.iterator();
while (it.hasNext()) {
ResolveInfo localResolveInfo = it.next();
String str = localResolveInfo.activityInfo.packageName;
if (imageSupportingApps.contains(str) && (imageUri != null)) {
Intent localIntent = new Intent(Intent.ACTION_SEND);
localIntent.setComponent(new ComponentName(str, localResolveInfo.activityInfo.name));
localIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
localIntent.setType("image/*");
localIntent.putExtra(Intent.EXTRA_SUBJECT, title);
localIntent.putExtra(Intent.EXTRA_TEXT, title + "\n" + message);
localIntent.setPackage(str);
mainIntentList.add(localIntent);
}
}
Intent in = Intent.createChooser(shareIntent, "Share on ..");
in.putExtra(Intent.EXTRA_INITIAL_INTENTS, mainIntentList.toArray(new Parcelable[localArrayList1.size()]));
activity.startActivity(in);
更新 1: 这是我到目前为止所尝试的。但它只是发送文本。它不与 whatsapp 共享图像。