Android Question File generation, slower than sending by mail?

netsistemas

Active Member
Licensed User
Longtime User
I have a process that generates an HTML with images, and later, I generate a PDF that, in turn, I send by email and upload to FTP.

Usually it worked fine, but in some cases, the mail is attached to the mail, but disappears when sent.

In one case, we came to see in the android outlook, which is the application we use, a message like 'We are still working on your attachments. If you send the message now, it won't be included'.

All the generation processes are synchronous or in case of being asynchronous, I use wait for. It gives the feeling that at some point, the file is not yet generated, and is already being sent, without actually existing 100%. Java, I think, recognizes a file from the first existing bit, and doesn't know if it's fully generated or not.
The files can weigh 3 mb or more.
 

netsistemas

Active Member
Licensed User
Longtime User
B4X:
Private sendmail As clsSendMail
....
....
sendmail.Send("Parte",GetParteActivoTXT,  Starter.Provider.SharedFolder,  Cuerpo,Asunto,File2Send,M0)

and this is my class (clsSendMail):




B4X:
      EmailIntent.Initialize(EmailIntent.ACTION_SEND, SendTo)

all my class:

(insert the file sending in a remotedatabase, but this is not the problem)

B4X:
'https://www.b4x.com/android/forum/threads/class-send-e-mail-with-fileprovideruri.89217/#content

'añadir a manifes:
''E-Mail senden ab Android 6+
'AddManifestText(<uses-permission
'    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
'    android:maxSdkVersion="18" />
')
'
'AddApplicationText(
'  <provider
'  android:name="android.support.v4.content.FileProvider"
'  android:authorities="$PACKAGE$.provider"
'  android:exported="false"
'  android:grantUriPermissions="true">
'  <meta-data
'  android:name="android.support.FILE_PROVIDER_PATHS"
'  android:resource="@xml/provider_paths"/>
'  </provider>
')
'CreateResource(xml, provider_paths,
'   <external-files-path name="name" path="" />
')
''Ende of File Provider

Sub Class_Globals
'    Private rp As RuntimePermissions

  
    Private GlobPath As String
    Private GlobFileName As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
  
End Sub

Public Sub Send(Tipo As String ,Parte As String , Directorio As String, body As String, SendSub As String, Attachments As String, SendTo As String )
    Dim SendAtt As String = ""
  

'    Create Table Partes_Mails(IDRegistro bigint identity(1,1), Parte nvarchar(20), SendBody nvarchar(255), SendSub nvarchar(255), Adjuntos nvarchar(255),
'    MailsDestino nvarchar(255), F_Envio DateTime Default GetDate(), Usuario int, Primary Key (IDRegistro))
'
'
'
'    create procedure APP_MailEnviado(@Parte nvarchar(20), @SendBody nvarchar(255), @SendSub nvarchar(255), @Adjuntos nvarchar(255), @MailsDestino nvarchar(255),
'    @Usuario int)
'    As
'    set nocount on
'    Insert into Partes_Mails(Parte, SendBody, SendSub, Adjuntos, MailsDestino, Usuario)
'    Values (@Parte, @SendBody, @SendSub, @Adjuntos, @MailsDestino, @Usuario)
' 
    Try
                      
        Dim Sql As String
        Sql = "APP_MailEnviado ?,?,?,?,?,?,?"
        Dim M As List
        M.Initialize
        M.Add(Tipo)
        M.Add(Parte)
        M.Add(body)
        M.Add(SendSub)
        M.Add(Attachments)
        M.Add(SendTo)
        M.Add(modGeneral.V_Usuario.Codigo)
        modGeneral.clsConnec.Execute2(Sql,M,True)
    Catch
        Log(LastException)
        MsgboxAsync(LastException.Message,"Cuando pueda , AVISE de esta incidencia: 'El MAIL no se guardó en la Base de Datos'. Gracias.")
    End Try
      
    GlobPath = Directorio
    GlobFileName = Attachments
  
    'Dim SendTo As String = "" '"[email protected]"
  
    If Attachments <> "" Then
        If GlobPath.ToLowerCase <> Starter.provider.SharedFolder.ToLowerCase Then
            File.Copy(GlobPath, GlobFileName, Starter.provider.SharedFolder, GlobFileName) 'Put the file in a shared space ready for attaching
        End If
        SendAtt = File.Combine(GlobPath, GlobFileName)
    End If
  



  
    SendEmail (SendTo, body,  SendSub, SendAtt )
End Sub

Private Sub SendEmail (SendTo As String, SendBody As String, SendSub As String, SendAtt As String)
    Dim Linea As String
    Try
      
        Dim EmailIntent As Intent
        Linea = "1"
      
        'LogColor("Send Att: " & SendAtt, Colors.Red)
        EmailIntent.Initialize(EmailIntent.ACTION_SEND, SendTo)
    '    EmailIntent.Initialize(EmailIntent.ACTION_SEND, Uri.fromParts(
    '    "mailto","[email protected]", Null))
      

        Dim LDestinos() As String
        Linea = "10"
        LDestinos = Regex.Split( ";",SendTo)
        Linea = "20"
        EmailIntent.SetType("text/plain") 'it is not related to the file itself.
        Linea = "30"
        EmailIntent.Flags = 1
        Linea = "40"
        If SendAtt <> "" Then
            Linea = "100"
            EmailIntent.PutExtra("android.intent.extra.STREAM", Starter.provider.GetFileUri(GlobFileName))
        End If
        Linea = "110"
        EmailIntent.putExtra("android.intent.extra.SUBJECT", SendSub)
        Linea = "120"
        EmailIntent.putExtra("android.intent.extra.TEXT", SendBody)
        Linea = "130"
        EmailIntent.putExtra("android.intent.extra.EMAIL", LDestinos)
        Linea = "200"


        StartActivity (EmailIntent)
        Linea = "300"
    Catch
        MsgboxAsync("Falló el envio del mail. Linea: " & Linea & CRLF & LastException.Message,"Error al enviar mail")
        Log(LastException)
    End Try

End Sub

Private Sub CreateFileProviderUri_deprecated (Dir As String, FileName As String) As Object
    Dim FileProvider2 As JavaObject
    Dim context As JavaObject
    context.InitializeContext
    FileProvider2.InitializeStatic("android.support.v4.content.FileProvider")
    Dim f As JavaObject
    f.InitializeNewInstance("java.io.File", Array(Dir, FileName))
    Return FileProvider2.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f))
End Sub
 
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
1# create html
2# view en webview
3# upload html to ftp
4# create pdf
5# send pdf my mail
6# upload pdf to fpg


The file send is the PDF created with :

B4X:
phtmltopdf.Initialize("phtmltopdf")

phtmltopdf.ConvertFromFile(Starter.Provider.SharedFolder, Starter.htmlTmpGenerado, Starter.Provider.SharedFolder , File2Send)

 Wait For phtmltopdf_Finished (Success As Boolean)




B4X:
'(NewHtml have then text html)

sub GenerarHTML
...
...
...
Dim bytes() As Byte = NewHTML.GetBytes("UTF8")

        File.WriteBytes(Starter.Provider.SharedFolder,Starter.htmlTmpGenerado,bytes)
        Dim Fichero As String
        Fichero = File.Combine( Starter.Provider.SharedFolder,Starter.htmlTmpGenerado)
   

       
        Return Fichero
end sub




'and pseudocode:


sub miFunctionSAMPLECodedhere 'all code are more expensaive

'take sign in SignaturePat
SignaturePad.saveBitmapToJPG(bmp,File.Combine(Starter.Provider.SharedFolder, FileName))

'genera file html with html base, images and sign: (10 photos by sample and sift -> 2mb aprox.
fichero = GenerarHTML

'load file html in webview
modHTMLMontar.MostrarFicheroEnWebView(FicheroFinal, WebViewVistaPrevia)

'and finally: upload to ftp and send mail
'send html by ftp:
..


'generate PDF (THIS FILE ARE THE SEND by mail)
'Private  phtmltopdf As PalmoHtmlToPdf  ' defined in GLOBAL area
File2Send = CodeDoc   & ".pdf"
                   
phtmltopdf.Initialize("phtmltopdf")
phtmltopdf.ConvertFromFile(Starter.Provider.SharedFolder, Starter.htmlTmpGenerado, Starter.Provider.SharedFolder , File2Send)
 Wait For phtmltopdf_Finished (Success As Boolean)


'and finally send mail: (first code in message)
sendmail.Initialize
'....
sendmail.Send("Parte",GetParteActivoTXT,  Starter.Provider.SharedFolder,  Cuerpo,Asunto,File2Send,M0)
 
'and upload PDF to ftp:
modGeneral.EnviarFicheroPor_FTPPLUS(Starter.Provider.SharedFolder, File2Send,"PDF","PDF")
 
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
One last attempt has been to add a check to make sure the file is fully created.
I check that the file size is not growing, and if it is, I wait.
Another possibility had thought about the filecopy.
However, we have decided to remove this part, and send the file in another way from the server, as we cannot assume these failures. Customer service is a priority in this company.
Thanks. If I discover something, I'll add it to this post, but I'll close it.

Code added after generating the PDF
B4X:
Dim Esperas As Int
            Esperas= -1
            Do While Flag = True
        
                Size = File.Size(Starter.Provider.SharedFolder,File2Send)
                'Sleep(100) 'may be, i put this line active
                If Size = SizePrevio Then   
                    Flag = False
                Else
                    Esperas = Esperas + 1
                End If
                SizePrevio = Size
                
                If Esperas > 50 Then
                    Flag = False
                End If
            Loop
 
Upvote 0
Top