Android:将图片附加到电子邮件意图 - 错误

时间:2016-01-19 19:34:09

标签: android email android-intent

我正在尝试附加图片文件以发送意图(GMail等...) 这是我的代码:

public void mail(ReadableMap options, Callback callback) {
Intent i = new Intent(Intent.ACTION_SEND);
  i.setType("application/image");

  if (options.hasKey("subject") && !options.isNull("subject")) {
  i.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"));
}

if (options.hasKey("body") && !options.isNull("body")) {
  i.putExtra(Intent.EXTRA_TEXT, options.getString("body"));
}

if (options.hasKey("recipients") && !options.isNull("recipients")) {
  ReadableArray r = options.getArray("recipients");
  int length = r.size();
  String[] recipients = new String[length];
  for (int keyIndex = 0; keyIndex < length; keyIndex++) {
    recipients[keyIndex] = r.getString(keyIndex);
  }
  i.putExtra(Intent.EXTRA_EMAIL, recipients);
}

if (options.hasKey("attachment") && !options.isNull("attachment")) {
  ReadableArray r = options.getArray("attachment");
  Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), r.getString(0)));
//      Log.e("FILE", r.getString(0));
  i.putExtra(Intent.EXTRA_STREAM, uri);
}

Intent chooser = Intent.createChooser(i, "Send Mail");

chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
  reactContext.startActivity(chooser);
} catch (Exception ex) {
  callback.invoke("error");
}
}

日志返回:

  

01-19 21:26:52.204 19326-19474 / com.nevermind.app E / FILE:file:///storage/emulated/0/Pictures/e461e1a3-b000-4201-8371-00f5e61eca8c.jpg

图像在设备上有效,但它返回错误:无法附加空文件。其余的电子邮件字段在GMail应用程序上运行良好。

以下是Android Studio的输出:

  

MultiPhoneWindowManager.shouldEnableLayoutInsetsBySoftInput:-1   01-19 21:35:14.864 1225-13155 / system_process E / ClipboardUIManager:无法获得ClipboardService   01-19 21:35:14.874 3840-3840 / com.google.android.gm E / Gmail:添加附件时出错                                                               com.android.mail.utils.b:无法附加空附件                                                                   在com.android.mail.ui.ComposeAttachmentTileGrid.a(SourceFile:62)                                                                   在com.android.mail.compose.c.b(SourceFile:2098)                                                                   在com.android.mail.compose.c.c(SourceFile:2228)                                                                   在com.android.mail.compose.c.a(SourceFile:9157)                                                                   在com.android.mail.compose.c.w(SourceFile:833)                                                                   在com.android.mail.compose.c.onCreate(SourceFile:5904)                                                                   在com.google.android.gm.ComposeActivityGmail.onCreate(SourceFile:165)                                                                   在android.app.Activity.performCreate(Activity.java:6550)                                                                   在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)                                                                   在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3102)                                                                   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)                                                                   在android.app.ActivityThread.access $ 1000(ActivityThread.java:197)                                                                   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1681)                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                   在android.os.Looper.loop(Looper.java:145)                                                                   在android.app.ActivityThread.main(ActivityThread.java:6891)                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                   在java.lang.reflect.Method.invoke(Method.java:372)                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1404)                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

1 个答案:

答案 0 :(得分:2)

Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), r.getString(0)));

更改为:

Uri uri = Uri.parse(r.getString(0));

R.getSting已包含完整路径,因此无需添加外部存储。