发送多个附件firemonkey

时间:2019-09-11 20:25:15

标签: delphi firemonkey

我正在尝试发送带有多个附件的电子邮件,但是,在最近的Android SDK中,已更改了发送文件的方法,并且大多数现有教程都是关于旧版SDK ...

我只发送了一个附件,但没有多个附件...

我尝试使用与1个附件相同的方法,但使用两次。无效,delphi仅捕获最后一个Intent.putExtra

我做错了什么?

(仅使用Android Plataform)

procedure TF_inicio.Rec_exportClick(Sender: TObject);
var
  sfilename: string;

  AttachmentFile1: JFile;
  AttachmentFile2: JFile;
  AttachmentFile3: JFile;

  Intent: JIntent;
  AddressesTo, Anexos: TJavaObjectArray<JString>;
  LList: TStringDynArray;
  I: Integer;
  LSearchOption: TSearchOption;
  // AddressesCC, AddressesBCC: TJavaObjectArray<JString>;
begin
  LSearchOption := TSearchOption.soTopDirectoryOnly;
  LList := TDirectory.GetFiles(path, '*.csv', LSearchOption);

  Intent := TJIntent.Create;
  Intent.setAction(TJIntent.JavaClass.ACTION_SENDTO);
  Intent.setData(TJnet_Uri.JavaClass.parse(StringToJString('mailto:')));
  AddressesTo := TJavaObjectArray<JString>.Create(1); // Enviar para:
  AddressesTo.Items[0] := StringToJString('MyEmail@gmail.com');
  Intent.putExtra(TJIntent.JavaClass.EXTRA_EMAIL, AddressesTo);
  Intent.putExtra(TJIntent.JavaClass.EXTRA_SUBJECT,
    StringToJString(F_inicio.placa_veiculo + ' - ' +
    FormatDateTime('dd/mm/yyyy hh:MM', NOW)));
  Intent.putExtra(TJIntent.JavaClass.EXTRA_TEXT,
    StringToJString('Email automático' + sLineBreak + ''));

  if Length(LList) = 1 then
    AttachmentFile1 := TJFile.JavaClass.init(StringToJString(LList[1]));

  if Length(LList) = 2 then
    AttachmentFile2 := TJFile.JavaClass.init(StringToJString(LList[2]));

  if Length(LList) = 3 then
    AttachmentFile3 := TJFile.JavaClass.init(StringToJString(LList[3]));

  if Length(LList) >= 1 then
  begin
    AttachmentFile1 := TJFile.JavaClass.init(StringToJString(LList[1]));
    Intent.putExtra(TJIntent.JavaClass.EXTRA_STREAM,
      TJParcelable.Wrap((TJnet_Uri.JavaClass.fromFile(AttachmentFile1)
      as ILocalObject).GetObjectID));
  end;

  if Length(LList) >= 2 then
  begin
    AttachmentFile2 := TJFile.JavaClass.init(StringToJString(LList[2]));
    Intent.putExtra(TJIntent.JavaClass.EXTRA_STREAM,
      TJParcelable.Wrap((TJnet_Uri.JavaClass.fromFile(AttachmentFile2)
      as ILocalObject).GetObjectID));
  end;

  if Length(LList) >= 3 then
  begin
    AttachmentFile3 := TJFile.JavaClass.init(StringToJString(LList[3]));
    Intent.putExtra(TJIntent.JavaClass.EXTRA_STREAM,
      TJParcelable.Wrap((TJnet_Uri.JavaClass.fromFile(AttachmentFile3)
      as ILocalObject).GetObjectID));
  end;

  SharedActivity.startActivity(Intent);
end;

1 个答案:

答案 0 :(得分:1)

只需使用简单的循环添加附件,如果不需要块,则所有这些附件都是多余的:

Intent.putExtra(Parsable[])

或者,使用一次对Intent.putParcelableArrayListExtra()Intent.putExtra(Parsable)的调用来添加URI数组,而不是为每个单独的URI调用procedure TF_inicio.Rec_exportClick(Sender: TObject); var LList: TStringDynArray; LSearchOption: TSearchOption; Intent: JIntent; I: Integer; AttachmentFile: JFile; LUri: Jnet_Uri; LURIs: TJavaObjectArray<JParcelable>; begin LSearchOption := TSearchOption.soTopDirectoryOnly; LList := TDirectory.GetFiles(path, '*.csv', LSearchOption); Intent := TJIntent.Create; Intent.setAction(TJIntent.JavaClass.ACTION_SEND_MULTIPLE); Intent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK); Intent.setData(TJnet_Uri.JavaClass.parse(StringToJString('mailto:'))); Intent.setType(StringToJString('vnd.android.cursor.dir/email')); Intent.putExtra(TJIntent.JavaClass.EXTRA_EMAIL, StringToJString('MyEmail@gmail.com')); Intent.putExtra(TJIntent.JavaClass.EXTRA_SUBJECT, StringToJString(F_inicio.placa_veiculo + ' - ' + FormatDateTime('dd/mm/yyyy hh:MM', NOW))); Intent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString('Email automático' + sLineBreak + '')); if LList <> nil then begin LURIs := TJavaObjectArray<JParcelable>.Create(Length(LList)); for I := 0 to Length(LList)-1 do begin AttachmentFile := TJFile.JavaClass.init(StringToJString(LList[I])); LUri := TJnet_Uri.JavaClass.fromFile(AttachmentFile); LURIs[I] := TJParcelable.Wrap((LUri as ILocalObject).GetObjectID); end; Intent.putExtra(TJIntent.JavaClass.EXTRA_STREAM, LURIs); end; SharedActivity.startActivity(Intent); end;

procedure TF_inicio.Rec_exportClick(Sender: TObject);
var
  LList: TStringDynArray;
  LSearchOption: TSearchOption;
  Intent: JIntent;
  I: Integer;
  AttachmentFile: JFile;
  LUri: Jnet_Uri;
  LURIs: JArrayList;
begin
  LSearchOption := TSearchOption.soTopDirectoryOnly;
  LList := TDirectory.GetFiles(path, '*.csv', LSearchOption);

  Intent := TJIntent.Create;
  Intent.setAction(TJIntent.JavaClass.ACTION_SEND_MULTIPLE);
  Intent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
  Intent.setData(TJnet_Uri.JavaClass.parse(StringToJString('mailto:')));
  Intent.setType(StringToJString('vnd.android.cursor.dir/email'));
  Intent.putExtra(TJIntent.JavaClass.EXTRA_EMAIL, StringToJString('MyEmail@gmail.com'));
  Intent.putExtra(TJIntent.JavaClass.EXTRA_SUBJECT, StringToJString(F_inicio.placa_veiculo + ' - ' + FormatDateTime('dd/mm/yyyy hh:MM', NOW)));
  Intent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString('Email automático' + sLineBreak + ''));

  if LList <> nil then
  begin
    LURIs := TJArrayList.Create;
    for I := 0 to Length(LList)-1 do
    begin
      AttachmentFile := TJFile.JavaClass.init(StringToJString(LList[I]));
      LUri := TJnet_Uri.JavaClass.fromFile(AttachmentFile);
      LURIs.add(LUri); 
    end;
    Intent.putParcelableArrayListExtra(TJIntent.JavaClass.EXTRA_STREAM, LURIs);
  end;

  SharedActivity.startActivity(Intent);
end;
{{1}}