Message.Attachments.Add issue

nmoscardo

Member
Licensed User
Longtime User
GoodMorning,

I have in my Android Emulator one file PNG call File.Png (/sdcard/File.png).
When a click on my button, I have this code below.
This code must generate one email whith my email address and attach my file on it. The emulator show the new form email where the body and subject is ok, but there isn't no file attach on it.

Dim Message As Email
Message.To.Add("myemail@mydomain.com")
Message.Body = "Ciao"
Message.Subject = "This Is My File.PNG."
Message.Attachments.Add(File.Combine(File.DirRootExternal, "Firma.PNG"))

StartActivity(Message.GetIntent)

I don't know what's is the wrong code part.
I use the example show on the documentation.
My Emulator platform is Android 2.1 update 1.

Thanks :sign0085:
 

kickaha

Well-Known Member
Licensed User
Longtime User
The line
B4X:
Message.Attachments.Add(File.Combine(File.DirRootExternal, "Firma.PNG"))
Should be
B4X:
Message.Attachments.Add(File.Combine(File.DirRootExternal, "File.png"))
 
Upvote 0

nmoscardo

Member
Licensed User
Longtime User
This is my source code for reproduce the issue.

This is my source code.
The canvas image is create without issue but the new email form is create without the attch file.


Sub Process_Globals
Dim InputStream1 As InputStream
End Sub

Sub Globals
Dim Canvas1 As Canvas
End Sub

Sub Activity_Create(FirstTime As Boolean)
Canvas1.Initialize(Activity)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub ButtonSalvaFirma_Click
Dim Out As OutputStream
If File.Exists(File.DirRootExternal,"Firma.png") Then
File.Delete(File.DirRootExternal,"Firma.png")
End If
Out = File.OpenOutput(File.DirRootExternal , "Firma.png" , False)
canvas1.Bitmap.WriteToStream(Out , 100 , "PNG")
Out.Close

Dim Message As Email
Message.To.Add("n.moscardo@edpinfoservice.it")
Message.Body = "Ciao"
Message.Subject = "INVIO FOTO SERIAL NUMBER."
Message.Attachments.Add(File.Combine(File.DirRootExternal, "Firma.png"))
StartActivity(Message.GetIntent)
End Sub
 

Attachments

  • Foto.JPG
    Foto.JPG
    14.3 KB · Views: 327
Upvote 0

nmoscardo

Member
Licensed User
Longtime User
Also in this context I have the same issue:

camera1.StartPreview
Dim out As OutputStream
out = File.OpenOutput(File.DirRootExternal, "1.jpg", False)
out.WriteBytes(data, 0, data.Length)
out.Close
ToastMessageShow("Image saved: " & File.Combine(File.DirRootExternal, "1.jpg"), True)

Dim Message As Email
Message.To.Add("n.moscardo@edpinfoservice.it")
Message.Body = "Ciao"
Message.Subject = "INVIO FOTO SERIAL NUMBER."
Message.Attachments.Add(File.Combine(File.DirRootExternal, "1.jpg"))

StartActivity(Message.GetIntent)

:sign0085:
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Is the file "Firma.png" actually being created - it is worth checking that it exists:
B4X:
Sub ButtonSalvaFirma_Click
Dim Out As OutputStream
If File.Exists(File.DirRootExternal,"Firma.png") Then
File.Delete(File.DirRootExternal,"Firma.png")
End If
Out = File.OpenOutput(File.DirRootExternal , "Firma.png" , False)
canvas1.Bitmap.WriteToStream(Out , 100 , "PNG")
Out.Close

Dim Message As Email
If File.Exists(File.DirRootExternal,"Firma.png") Then Msgbox ("File found", "Working")
Message.To.Add("n.moscardo@edpinfoservice.it")
Message.Body = "Ciao"
Message.Subject = "INVIO FOTO SERIAL NUMBER."
Message.Attachments.Add(File.Combine(File.DirRootE xternal, "Firma.png"))
StartActivity(Message.GetIntent) 
End Sub
 
Upvote 0

nmoscardo

Member
Licensed User
Longtime User
Like you see in the Attach Picture, the file exists.
I have add the code +

If File.Exists(File.DirRootExternal,"Firma.png") Then Msgbox ("File found", "Working")

and the message box show the message "File found".
It 's very strange, because I use the Android emulator then something standard with the example of libraries and do not receive any error.

But it is a big problem for me because I have to develop a software that sends photos by email.

:sign0085:
 

Attachments

  • Foto.JPG
    Foto.JPG
    11.6 KB · Views: 302
Upvote 0

nmoscardo

Member
Licensed User
Longtime User
I have test with the emulatore installed on a PC without domain restriction and I use the administrator user.
Also I test the software on 3 different tablet and I have the same issue.
For me is a Bug software library.

No one Help me ?

:sign0085:
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
I have almost the same mail sending sub as yours in one of my programs:
B4X:
Sub ButtonSaveSendMail_Click
   Dim Message As Email
   Dim savedir, savefile As String   
   savefile= EditTextSaveFilename.Text & ".csv"
   savedir = File.DirRootExternal & "/AndyPad"
   
   If File.Exists (savedir, savefile) = False Then
      Msgbox2 ("Cannot find file, please re-save.", "Error","","OK","",bd.Bitmap)
      Return
   End If

   Message.To.Add(EditTextSaveAddress.Text)
   Message.Attachments.Add(File.Combine(savedir, savefile))
   Message.body = EditTextSaveEmailBody.Text 
   StartActivity(Message.GetIntent) 

End Sub
and it works fine across a number of devices. I am stuck here.:sign0013:
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Just a thought, but have you tried
B4X:
Out.Flush
to ensure the file is fully written and closed
 
Upvote 0

nmoscardo

Member
Licensed User
Longtime User
With Eclipse all run Well !!!

I have open my Eclipse and I have develop a simple send email solution (I have attach the source code).

I have compiled with Eclipse and installed the APK on my TABLET Android and all run very well.

The software send email with the email software and also with Gmail software.

If with Eclipse I have no isseu I think that the better way is see my source code e see where is different in the source code libraru of Basic4Android.

What do you mean ?
 

Attachments

  • TestEmail.zip
    44.9 KB · Views: 276
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've tested your code on three real devices and it worked correctly (the file gets attached).
The Java code you wrote is different than the code in the Phone library, because the phone library supports multiple attachments and your code supports a single attachment.

You can however easily create the same intent as the one you created with Eclipse using the Intent object if you only need to attach a single file.
 
Upvote 0

nmoscardo

Member
Licensed User
Longtime User
Info

Thanks Erel,
but my issue is that whit Basic4android I can't attach file and with Eclipse source all work fine.
I want understand why I can't attach file with the code show below but with Eclipse/java code alla run weel.
My eclipse code attach only one file because is a small example only create for see if I have or not general problem in my configuration PC (emulatore , java runtime and soo).


Dim out As OutputStream
out = File.OpenOutput(File.DirRootExternal, "1.jpg", False)
out.WriteBytes(data, 0, data.Length)
out.Close
ToastMessageShow("Image saved: " & File.Combine(File.DirRootExternal, "1.jpg"), True)

Dim Message As Email
Message.To.Add("n.moscardo@edpinfoservice.it")
Message.Body = "Ciao"
Message.Subject = "INVIO FOTO SERIAL NUMBER."
Message.Attachments.Add(File.Combine(File.DirRootExternal, "1.jpg"))

StartActivity(Message.GetIntent)
 
Upvote 0

nmoscardo

Member
Licensed User
Longtime User
Conversion

I have convert my Eclipse code but it doesn't Run.
Please where is the wrong code ?


'BASIC4android code
Dim Intent1 As Intent
Intent1.Initialize(Intent1.ACTION_SEND, "")
Intent1.putExtra("android.content.Intent.EXTRA_EMAIL", "n.moscardo@edpinfoservice.it")
Intent1.putExtra("android.content.Intent.EXTRA_SUBJECT", "Hello")
Intent1.setType("image/png")
Intent1.putExtra("Intent.EXTRA_STREAM", "file:///sdcard/Firma.png")
Intent1.putExtra("android.content.Intent.EXTRA_TEXT", "Hello.")

StartActivity(Intent1)


'Eclipse
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.extra.EMAIL, new String[]{ address.getText().toString()});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext.getText());

Uri attachment = Uri.parse("file:///sdcard/Firma.png");
emailIntent.putExtra(Intent.EXTRA_STREAM, attachment);

Form.this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
 
Upvote 0

CharlesR

Member
Licensed User
Longtime User
I don't know if this was ever resolved, but I have a similar problem. I am trying to email an attachment based on the code in the example but whilst the email is working the attachment is not. I am probably doing something very silly, but can anyone see what is wrong with this code.

Many thanks

Charles

Sub Activity_Create(FirstTime As Boolean)
'Activity.LoadLayout("Mailtest")
If File.ExternalWritable = False Then
Msgbox("Cannot write on storage card.", "")
Return
End If
SaveStringExample
SendMail
End Sub

Sub SaveStringExample
File.WriteString(File.DirRootExternal, "Report.txt", _
"Pool hall volume = 7164 Air Temp = 29.0 Air changes/hr = 1.15" & CRLF & "Annual Heat Loss = 473578.38 kWh")
End Sub

Sub SendMail
Dim Message As Email
Dim savedir, savefile , receiver, subject, body As String
receiver = "xyz@gmail.com"
subject = "Test Report - 3 June 2011"
body = "Pool hall volume = 7164 Air Temp = 29.0 Air changes/hr = 1.15 Annual Heat Loss = 473578.38 kWh JUST A DEMO!"
savefile= "Report.txt"
savedir = File.DirRootExternal
Message.To.Add(receiver) 'Email address
Message.Subject = subject 'Email subject
Message.body = body 'Email main text
If File.Exists(File.DirRootExternal,"Report.txt") Then Msgbox ("File found", "Working")
Message.Attachments.Add(File.Combine(savedir,savefile))
StartActivity(Message.GetIntent)
End Sub
 
Upvote 0

CharlesR

Member
Licensed User
Longtime User
I have tried an alternative intent, but it is beyond me because I am way out of my depth! Do you think that the problem may be with gmail, and can you recommend an alternative email service which works well with B4A. I just need to be able to receive a report when my app has been run and to be honest it does not matter what email provider is used.

Thanks

Charles
 
Upvote 0
Top