Question

I've found this code to send multiple attachments using 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..."));
}

But there's a problem, for one attachment i already did it, but i can't figure out how to convert this part to Delphi. This is where you can add two or more files in attachment.

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

In this case I tried this:

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;

But the app raises a Access Violation when I add the first line of the JArrayValue.

Was it helpful?

Solution

just did it...

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top