【发布时间】:2014-05-20 16:32:38
【问题描述】:
我想使用 Intent 仅通过电子邮件发送照片。我正在使用下面的代码,但它不仅打开了 gmail,还显示了许多共享选项。
请帮我分享唯一的gmail。
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg"); // put here your mime type
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
if(!resInfo.isEmpty()) {
Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
ArrayList<Uri> uris = new ArrayList<Uri>();
for (ResolveInfo info : resInfo) {
if(info.activityInfo.packageName.toLowerCase().contains("gmail") || info.activityInfo.name.toLowerCase().contains("gmail")) {
targetedShare.setType("image/jpeg"); // put here your mime type
targetedShare.putExtra(Intent.EXTRA_SUBJECT, "Amplimesh Photo");
targetedShare.putExtra(Intent.EXTRA_TEXT,"Attached the Quote");
//Fetching the Installed App and open the Gmail App.
for(int index = 0; index < productList.size(); index++) {
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(productList.get(index).getOverlayBitmap());
Bitmap overLayBitmap = BitmapFactory.decodeStream(byteInputStream);
String fileName = SystemClock.currentThreadTimeMillis() + ".png";
//Save the bitmap to cache.
boolean isSaved = Helper.saveImageToExternalStorage(overLayBitmap, getApplicationContext(), fileName);
if(isSaved)
uris.add(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/amplimesh/images/" + fileName)));
}
}
}
targetedShare.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivityForResult(Intent.createChooser(targetedShare, "Sending multiple attachment"), 12345);
}
【问题讨论】:
-
@SanketKachhela 它显示了许多选项,例如 Skype、Gmail、Office Suite wifi Direct。我只想在 gmail 上分享。
-
@user2695306 所以......你必须在你的清单文件中给出一个特定的意图操作......
-
@PiyushGupta 请帮助我怎么能这样
-
两点... 1. 您使用的是
startActivityForResult(...)- 我可能错了,但我不确定GMail 应用程序是否真的会返回结果。 2. 然而,重点是您使用的是Intent.createChooser,它(顾名思义)会自动创建一个已安装应用程序列表,这些应用程序已注册以对特定 MIME 类型执行特定操作。