如何使用Intent发送多个附件

时间:2014-04-28 04:55:18

标签: android delphi android-intent firemonkey delphi-xe5

我发现此代码使用Gmail Android App发送多个附件:

public static void email(Context context, String emailTo, String emailCC,
    String subject, String emailText, List<String> filePaths)
{
    //need to "send multiple" to get more than one attachment
    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
        new String[]{emailTo});
    emailIntent.putExtra(android.content.Intent.EXTRA_CC, 
        new String[]{emailCC});
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject); 
    emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
    //has to be an ArrayList
    ArrayList<Uri> uris = new ArrayList<Uri>();
    //convert from paths to Android friendly Parcelable Uri's
    for (String file : filePaths)
    {
        File fileIn = new File(file);
        Uri u = Uri.fromFile(fileIn);
        uris.add(u);
    }
    emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}

但是有一个问题,对于我已经做过的一个附件,但我无法弄清楚如何将这个部分转换为Delphi。您可以在此处添加两个或多个附件中的文件。

 //has to be an ArrayList
    ArrayList<Uri> uris = new ArrayList<Uri>();
    //convert from paths to Android friendly Parcelable Uri's
    for (String file : filePaths)
    {
        File fileIn = new File(file);
        Uri u = Uri.fromFile(fileIn);
        uris.add(u);

在这种情况下我尝试了这个:

procedure TfrmSendMail.CreateEmail(const Recipient, Subject, Content,
  Attachment, Attachment2: string);
var
  Intent: JIntent;
  Uri: Jnet_Uri;
  Uri2: Jnet_Uri;
  AttachmentFile: JFile;
  AttachmentFile2: JFile;
  sl: JArrayList;
begin
  Intent := TJIntent.Create;
  Intent.setAction(TJIntent.JavaClass.ACTION_SEND_MULTIPLE);
  Intent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
  Intent.putExtra(TJIntent.JavaClass.EXTRA_EMAIL, StringToJString(Recipient));
  Intent.putExtra(TJIntent.JavaClass.EXTRA_SUBJECT, StringToJString(Subject));
  Intent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString(Content));

//  ShowMessage('incluindo arquivos');
  AttachmentFile := SharedActivity.getExternalFilesDir
    (StringToJString(Attachment));
  AttachmentFile2 := SharedActivity.getExternalFilesDir
    (StringToJString(Attachment2));

//  ShowMessage('criando URIs');
  Uri := TJnet_Uri.JavaClass.fromFile(AttachmentFile);
  Uri2 := TJnet_Uri.JavaClass.fromFile(AttachmentFile2);

//  ShowMessage('vai dar rolo aqui');

  sl.add(0,Uri);
  ShowMessage('e aqui?');
  sl.add(1,Uri2);

//  ShowMessage('nem chega aqui');
  Intent.putParcelableArrayListExtra(TJIntent.JavaClass.EXTRA_STREAM, sl);

  //  Intent.putExtra(TJIntent.JavaClass.EXTRA_STREAM,
//    TJParcelable.Wrap((Uri as ILocalObject).GetObjectID));


  Intent.setType(StringToJString('vnd.android.cursor.dir/email'));

  SharedActivity.startActivity(Intent);
end;

但是当我添加JArrayValue的第一行时,应用程序会引发访问冲突。

1 个答案:

答案 0 :(得分:3)

刚刚做到了......

procedure TfrmSendMail.CreateEmail(const Recipient, Subject, Content,
  Attachment, Attachment2: string);
var
  Intent: JIntent;
  Uri: Jnet_Uri;
  Uri2: Jnet_Uri;
  AttachmentFile: JFile;
  AttachmentFile2: JFile;
  Uris: JArrayList;
begin
  Intent := TJIntent.Create;
  Intent.setAction(TJIntent.JavaClass.ACTION_SEND_MULTIPLE);
  Intent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
  Intent.putExtra(TJIntent.JavaClass.EXTRA_EMAIL, StringToJString(Recipient));
  Intent.putExtra(TJIntent.JavaClass.EXTRA_SUBJECT, StringToJString(Subject));
  Intent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString(Content));

  AttachmentFile := SharedActivity.getExternalFilesDir
    (StringToJString(Attachment));
  AttachmentFile2 := SharedActivity.getExternalFilesDir
    (StringToJString(Attachment2));

  Uri := TJnet_Uri.JavaClass.fromFile(AttachmentFile);
  Uri2 := TJnet_Uri.JavaClass.fromFile(AttachmentFile2);

  Uris:= TJArrayList.Create;
  Uris.add(0,Uri);
  Uris.add(1,Uri2);

  Intent.putParcelableArrayListExtra(TJIntent.JavaClass.EXTRA_STREAM, Uris);

  Intent.setType(StringToJString('vnd.android.cursor.dir/email'));

  SharedActivity.startActivity(Intent);
end;