我使用以下代码在facebook,twitter,google +和其他社交应用上分享图片。图像从设备的外部存储上传。但是,当我在那里显示选择器时,会有多人脸书和推特图标。如何限制只显示一个图标?以下是代码。
public void customShare() {
if (file.exists()) {
Uri uri = Uri.fromFile(file);
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, uri);
List<ResolveInfo> resInfo = this.getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
targetedShareIntent.setType("image/jpeg");
targetedShareIntent.putExtra(Intent.EXTRA_STREAM, uri);
targetedShareIntent.setPackage(packageName);
if (packageName.equals("com.facebook.katana") || packageName.equals("ru.ok.android") ||
packageName.equals("com.vkontakte.android") || packageName.equals("com.twitter.android")||
packageName.equals("com.whatsapp") || packageName.equals("org.telegram.messenger") ||
packageName.equals("com.google.android.apps.plus")) {
targetedShareIntents.add(targetedShareIntent);
}
}
Intent chooserIntent = Intent.createChooser(
targetedShareIntents.remove(0), "");
chooserIntent.putExtra(
Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
}
答案 0 :(得分:0)
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, uri);
List<ResolveInfo> resInfo = this.getPackageManager().queryIntentActivities(intent, 0);
上面的代码重新调整了所有具有操作类型Intent.ACTION_SEND
的活动.Facebook有多种活动,此类型简单,因此resInfo
有多个具有相同packagename值的条目com.facebook.katana。使用resolveInfo.activityInfo.targetActivity
检查包含所有条目的活动
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
targetedShareIntent.setType("image/jpeg");
targetedShareIntent.putExtra(Intent.EXTRA_STREAM, uri);
targetedShareIntent.setPackage(packageName);
if (packageName.equals("com.facebook.katana") || packageName.equals("ru.ok.android") ||
packageName.equals("com.vkontakte.android") || packageName.equals("com.twitter.android") ||
packageName.equals("com.whatsapp") || packageName.equals("org.telegram.messenger") ||
packageName.equals("com.google.android.apps.plus")) {
Log.d("PackageAdded", packageName+" = ====== "+ resolveInfo.activityInfo.targetActivity);
targetedShareIntents.add(targetedShareIntent);
}
}
对于解决方案,您只需删除已添加到targetedShareIntents的现有包。