Android - 图像共享 Intent ,如 Androidify 应用程序

标签 android android-intent share

我想使用 Google 的 Androidify 应用程序中显示的相同选项创建图像共享 Intent :

enter image description here

我用 Intent.ACTION_SENDIntent.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:

enter image description here enter image description here

显然,Androidify 应用使用的方法是 Intent.ACTION_SENDIntent.ACTION_ATTACH_DATA 的混合。 有人知道怎么做吗?

感谢您的帮助!

最佳答案

最后我找到了怎么做,多亏了这个答案:

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);

关于Android - 图像共享 Intent ,如 Androidify 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20889968/

相关文章:

android - 错误包 `com.google.android.gms...` 不存在

java - 如何将 Json 解析为另一个 Activity

c++ - 如果程序从 linux 中的两个终端运行,fork 的共享内存是否共享

java - MainActivity 未收到 StartActivityForResult 的结果

javascript - 将文字设置为 google+ 分享按钮

android - 用标题、文本和链接填充 facebook 共享项目 (Android)

android - 在 Listview 中使用 ViewPager 时不会调用 OnItemClickListener

android - 从时间选择器中选择时间获取不同时区的时间

java - Android:将 ShowcaseView 作为项目导入时出现堆错误,如果作为 JAR,则为 ClassNotFound,内存不足

android - 清除堆栈,包括不同任务中的 Activity