gpt4 book ai didi

android - 如何使用 Intent 发送多个附件

转载 作者:行者123 更新时间:2023-11-29 17:50:21 25 4
gpt4 key购买 nike

我发现这段代码可以使用 Gmail Android 应用程序发送多个附件:

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 的第一行时,应用程序引发了访问冲突。

最佳答案

刚刚做了...

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;

关于android - 如何使用 Intent 发送多个附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23332750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com