Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Desarrollo en Delphi para Android (https://www.clubdelphi.com/foros/forumdisplay.php?f=57)
-   -   Problema para adjuntar archivo a un email (https://www.clubdelphi.com/foros/showthread.php?t=96128)

Esteban74 24-02-2023 04:09:03

Problema para adjuntar archivo a un email
 
Hola, espero alguien tenga una idea de como encarar esto, ya que no le veo solución y hay muy poca info al respecto, intento crear un email con información ya cargada y adjuntar un archivo de texto (*.csv) uso intents ya que indy no funciona pero obtengo un error que no puedo resolver.
Estoy usando Delphi Comunity Edition 10.4 y pruebo la app en un Moto G52 con android 12
Cito el código y el error a continuación:

Código Delphi [-]
procedure TDM.EnviarCorreo(const Destinatario, Asunto, Contenido, Adjunto: string);
{$IF DEFINED(ANDROID)}
var
  JRecipient: TJavaObjectArray;
  Intent: JIntent;
  Uri, Data: Jnet_Uri;
  AttachmentFile: JFile;
begin

  JRecipient := TJavaObjectArray.Create(1);
  JRecipient.Items[0] := StringToJString(Destinatario);
  Intent := TJIntent.Create;
  Intent.setAction(TJIntent.JavaClass.ACTION_SEND);
  Intent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
  Intent.putExtra(TJIntent.JavaClass.EXTRA_EMAIL, JRecipient);
  Intent.putExtra(TJIntent.JavaClass.EXTRA_SUBJECT, StringToJString(Asunto));
  Intent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString(Contenido));

  if Adjunto <> '' then
  begin
    if TJBuild_VERSION.JavaClass.SDK_INT >= TJBuild_VERSION_CODES.JavaClass.N then
    begin
      AttachmentFile := TJFile.JavaClass.init(StringToJString(Adjunto));
      Intent.addFlags(TJIntent.JavaClass.FLAG_GRANT_READ_URI_PERMISSION);
      Data := TJFileProvider.JavaClass.getUriForFile(TAndroidHelper.Context,
        StringToJString('com.FJAG.StatSuite.fileprovider'), AttachmentFile);
    end
    else
      Data := TJnet_Uri.JavaClass.parse(StringToJString('file://' + Adjunto));

    Intent.putExtra(TJIntent.JavaClass.EXTRA_STREAM,
      TJParcelable.Wrap((Data as ILocalObject).GetObjectID));
  end;

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

{$ENDIF}
{$IFDEF MSWINDOWS}
  mostrarMensaje('Caracteristica aun no soportada en windows');
{$ENDIF}
end;

Cito el error que se genera:
https://drive.google.com/file/d/1F_M...ew?usp=sharing

Agradecería cualquier ayuda al respecto. Gracias por su tiempo.

dani36652 02-03-2023 21:59:13

Antes que nada, recuerda que debes crear un archivo xml que contenga las rutas a las que el fileprovider tendrá acceso para crear el URI correspondiente a tu archivo.

quedando asi:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="internal_private_files" path="." />
<cache-path name="internal_cache_files" path="." />
<external-path name="external_public_files" path="." />
<external-files-path name="external_private_files" path="." />
<external-cache-path name="external_cache_files" path="." />
<external-media-path name="external_media_files" path="." />
</paths>

Recuerda también configurar el "remote path" de tu xml en las configuraciones de tu proyecto como "res\xml".
También tu Archivo "AndroidManifest_Template" debe tener la siguiente estructura:

<?xml version="1.0" encoding="utf-8"?>
<!-- BEGIN_INCLUDE(manifest) -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="%package%"
android:versionCode="%versionCode%"
android:versionName="%versionName%"
android:installLocation="%installLocation%">

<uses-sdk android:minSdkVersion="%minSdkVersion%" android:targetSdkVersion="%targetSdkVersion%" />
<%uses-permission%>
<uses-feature android:glEsVersion="0x00020000" android:required="True"/>
<application android:persistent="%persistent%"
android:restoreAnyVersion="%restoreAnyVersion%"
android:label="%label%"
android:debuggable="%debuggable%"
android:largeHeap="%largeHeap%"
android:icon="%icon%"
android:theme="%theme%"
android:hardwareAccelerated="%hardwareAccelerated%"
android:resizeableActivity="false"
android:requestLegacyExternalStorage="true">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.ejemplo.TuApp.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
<%application-meta-data%>
<%uses-libraries%>
<%services%>
<!-- Our activity is a subclass of the built-in NativeActivity framework class.
This will take care of integrating with our NDK code. -->
<activity android:name="com.embarcadero.firemonkey.FMXNativeActivity"
android:label="%activityLabel%"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
android:launchMode="singleTask">
<!-- Tell NativeActivity the name of our .so -->
<meta-data android:name="android.app.lib_name"
android:value="%libNameValue%" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<%activity%>
<%receivers%>
</application>
</manifest>
<!-- END_INCLUDE(manifest) -->

Finalmente el uso del fileprovider para el envío de un archivo mediante un intent se ve asi:
(Cabe mencionar que deberás adaptar el código para mandar el archivo vía correo electrónico)

Código Delphi [-]
procedure EnviarArchivo;
{$IFDEF ANDROID}
var
Log: JFile;
intent: JIntent;
UriLog: JNet_Uri;
Uris: JArrayList;
{$ENDIF}
begin
  try
    {Log Excepciones}
    if TFile.Exists(File_Path) then
    begin
    {$IFDEF ANDROID}
      Uris:= TJArrayList.Create;
      Log:= TJFile.JavaClass.init(StringToJString(File_Path));
      Log.setReadable(True, False);
      if TJBuild_VERSION.JavaClass.SDK_INT >= 24 then //Android 7.0 +
      begin
        UriLog:= TJFileProvider.JavaClass.getUriForFile(TAndroidHelper.Context,
        StringToJString(System.Concat(JStringToString(TAndroidHelper.Context.getPackageName),
        '.fileprovider')), Log);
        Uris.add(UriLog);
      end else
      begin
        UriLog:= TJnet_Uri.JavaClass.fromFile(Log);
        Uris.add(UriLog);
      end;

      intent:= TJIntent.Create;
      intent.setAction(TJIntent.JavaClass.ACTION_SEND_MULTIPLE);
      intent.addFlags(TJIntent.JavaClass.FLAG_GRANT_READ_URI_PERMISSION and
      TJIntent.JavaClass.FLAG_GRANT_WRITE_URI_PERMISSION);
      intent.putParcelableArrayListExtra(TJIntent.JavaClass.EXTRA_STREAM, Uris);
      intent.setType(StringToJString('text/plain'));
      TAndroidHelper.Activity.startActivity(TJIntent.JavaClass.createChooser(intent,
      StrToJCharSequence('Enviar Mediante..')));
    {$ENDIF}
    end else ShowMessage('No se encontró el Log');
  except on E: exception do
    begin
      EscribirLogExcepciones(E.ClassName +
      ' Error al tratar de enviar Archivo de Log: ' + E.Message);
    end;
  end;
end;

El código anterior lo tomé de una app donde el usuario selecciona por qué medio mandar los logs de operación y excepciones. Como te mencioné ya solo sería cuestión tuya adaptarlo para mandar el archivo por correo.


La franja horaria es GMT +2. Ahora son las 00:16:59.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi