Android Question I cannot embed an image in email at all - Android 14

hatzisn

Expert
Licensed User
Longtime User
Good evening to all of you,

I am trying to embed an image in an e-mail I create with html but it does not seem to care at all and it does not add it. I always see a square with the letters "OBJ" inside, in all e-mail apps. What do you thing might be happening? I have Android 14. Here is my code - the sub for the image encoder is copied and changed from an other member of the forum:

B4X:
Private Sub SendEmail(sJSON As String)
    Dim email As Email
    email.To.Add("")
    email.Subject = "This the subject"
    email.Body = GetHTMLForEmail(sJSON)
    StartActivity(email.GetHtmlIntent)
End Sub

Private Sub GetHTMLForEmail(sJSON As String) As String
    Dim sHTML As String = $"
    <center>
    <img alt="Top image" src=CHANGETOIMGTOP style="max-width:1402"></img>
    </center>
    "$
  
    sHTML = sHTML.Replace("CHANGETOIMGTOP", AddEmbededImageInMail("", "imgtop.png", Null, False))


    Log(sHTML)

    Return sHTML
End Sub

Public Sub AddEmbededImageInMail(strImagePathOrNullIfB4XBitmap As String,strImageNameOrNullIfB4XBitmap As String, bmpOrNullIfNoB4XBitmapIsUsed As B4XBitmap, ReturnIMGTag As Boolean) As String
    Dim Bytes() As Byte
    Dim su As StringUtils
  
    If bmpOrNullIfNoB4XBitmapIsUsed.IsInitialized = False Then
        'The path of the image.
        Dim strPath As String
        If strImagePathOrNullIfB4XBitmap.Trim = "" Then
            strPath = File.DirAssets     'ie.   ..../Files
        Else
            strPath = strImagePathOrNullIfB4XBitmap
        End If
      
        Bytes =  File.ReadBytes(strPath,strImageNameOrNullIfB4XBitmap)  'Read the image into an array of bytes.
    Else
      
        Bytes = ImageToBytes(bmpOrNullIfNoB4XBitmapIsUsed)
        Log(Bytes.Length)
    End If
  
 
    Dim strImageBase64 As String
    strImageBase64 = su.EncodeBase64(Bytes)   'Now Convert our Image to Base64 string so that it can be directly embedded into the HTML.
 
    'Prepare the image HTML 'src' property which will contain the Image data as Base64.
    Dim strImageSource As String = """" & "data:image/png;base64, " & strImageBase64 & """"    '    "data:image/png;base64, abc123//444553"
  

 
  
    If ReturnIMGTag Then
        Dim strHTML As String = ""
        strHTML =  "<img alt=""Picture"" src=" & strImageSource & " " & "/>"
        Return strHTML
    Else
        Return strImageSource
    End If
  
End Sub

Public Sub ImageToBytes(Image As B4XBitmap) As Byte()
    Dim out As OutputStream
    out.InitializeToBytesArray(0)
    Image.WriteToStream(out, 100, "PNG")
    out.Close
    Return out.ToBytesArray
End Sub
 

John Naylor

Active Member
Licensed User
Longtime User
Base64 images are not well supported in most popular email clients. Gmail & Outlook certainly block (or simply ignore) embedded Base64 images (spam & security reasons).

Could you host the image online somewhere and add it as a link? - something like this

B4X:
Dim sHTML As String = $"
<center>
<img alt='Top image' src='https://yourdomain.com/images/imgtop.png' style='max-width:1402' />
</center>
"$
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Base64 images are not well supported in most popular email clients. Gmail & Outlook certainly block (or simply ignore) embedded Base64 images (spam & security reasons).

Could you host the image online somewhere and add it as a link? - something like this

B4X:
Dim sHTML As String = $"
<center>
<img alt='Top image' src='https://yourdomain.com/images/imgtop.png' style='max-width:1402' />
</center>
"$

I get no result. The same thing happens.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Try this link to see what happens
B4X:
src='https://www.bing.com/rp/WolXT0CQYS0wYqf6yEezati6rBo.png'
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Try this link to see what happens
B4X:
src='https://www.bing.com/rp/WolXT0CQYS0wYqf6yEezati6rBo.png'

The same thing happens. A square with "OBJ" in the middle.
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
Most of email clients will block (by default) remote content.
You will see message like this:
1747897951132.png


You cannot see remote image as result:
1747898017977.png
 
Upvote 0
Top