Android Question Problem viewing image attachment POP3

bryon

Member
Licensed User
Longtime User
Good morning,
I'm using the Net library and also Erel's Mailparser module:

https://www.b4x.com/android/forum/threads/using-pop3-to-communicate-with-android-devices.11310/

No other changes have been done to the example except adding my login credentials.

All is well with downloading messages and images as attachments, however, when I attempt to view the image, it appears corrupted. I can see the file listing with a file manager app but it displays as a broken icon.

Can someone please try this and let me know if images are downloaded and displayed correctly on your device?

Any other ideas or comments would be appreciated.

Thanks,
Bryon
 

bryon

Member
Licensed User
Longtime User
..and then I found the following from this thread:
https://www.b4x.com/android/forum/threads/saving-a-picture.10200/#content

B4X:
 'send the intent that asks the media scanner to scan the file
    Dim i As Intent
    i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", _
        "file://" & File.Combine(File.DirdefaultExternal & "/momblog/images/",filename))
    Dim p As Phone
    p.SendBroadcastIntent(i)


But it didn't help. I did notice though, that the icon wen't from "broken" to a completely black icon.

Suggestions to try?
 
Upvote 0

bryon

Member
Licensed User
Longtime User
Thanks rboeck, that didn't help though. My files are downloading, B4A just can't seem to access them..
 
Upvote 0

bryon

Member
Licensed User
Longtime User
Thanks Erel, for responding.

Okay, so there is a difference between the downloaded file and the original.

I tried two images I had on my hard drive:
- "IdeaDrivedrawing.JPG"
- "Birds_flying.bmp"

First screenshot ("POP3_downloads[1].png") shows how the files show up on my device after POP3 download. These files would not display and when I tried initializing a bitmap, I encountered: "java.lang.RuntimeException: Error loading bitmap."

B4X:
Dim TestBitmap as bitmap
TestBitmap.Initialize(File.DirDefaultExternal & "/momblog/images/", MailParser.filename)

The original "IdeaDrivedrawing.JPG" had two additional bytes: FF and D9 at the end of the file that the POP3 downloaded file did not have.
The original "Birds_flying.bmp" had three additional bytes: 00, 00 and 00 at the end.

I loaded the POP3 versions into my hex editor and added the missing bytes, then copied them back to my phone where they then worked as expected. they also show up as images in my file manager too. (See next screenshot ("Files_fixed[1].png")

Somewhere they are losing some bytes.

I included the POP3 example with the mailparser module where I added media scanning code immediately after it saves the attachment in the Sub HandlePart().

Also, added the following line as per rboeck's suggestion previously in this thread:
B4X:
If nextPart = -1 Then nextPart = Mail.Length + 4
in Sub ParseMultipartBody().

Could you please have a look at the mailparser (or maybe even DecodeBase64) to see if there is something amiss?

My hardware is rooted Galaxy S5 connected to dev computer via USB cable. Will try same experiment at home with WiFi bridge.

Thank you.

-bryon
 

Attachments

  • POP3_downloads[1].png
    POP3_downloads[1].png
    139.8 KB · Views: 144
  • Files_fixed[1].png
    Files_fixed[1].png
    170.3 KB · Views: 145
  • POP3Test.zip
    8.9 KB · Views: 159
Upvote 0

bryon

Member
Licensed User
Longtime User
For completeness, I just tried with wifi bridge and with all the variations of debuggers and release compiles.

Same thing is happening. It's losing the last several bytes of the file.
 
Upvote 0

bryon

Member
Licensed User
Longtime User
Thanks Erel,

I found this:
B4X:
            If nextPart-4 > index Then
                HandlePart(headers.ToString, Mail.SubString2(index, nextPart-4), Msg)
            End If

and removed the if/then check and the -4 which left me with this:

B4X:
HandlePart(headers.ToString, Mail.SubString2(index, nextPart), Msg)

Which gives me the whole file. I don't know why the removal of 4 bytes is necessary. After you've found the boundary with
B4X:
nextPart = Mail.IndexOf2("--" & boundary, index)
isn't the complete file all you have left?

Can you explain why that check was in there please?

Thank you!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Does it work with this code?

B4X:
Sub ParseMultipartBody (Mail As String, Msg As Message)
   'find first boundary
   index = Mail.IndexOf2("--" & boundary, index)
   ReadNextLine(Mail)
   Dim headers As StringBuilder
   headers.Initialize
   Do While index < Mail.Length
     Dim line As String
     line = ReadNextLine(Mail)
     If line.Length > 0 Then
       headers.Append(line).Append(" ")
     Else If index < Mail.Length Then
       Dim nextPart As Int
       nextPart = Mail.IndexOf2("--" & boundary, index)
       If nextPart = -1 Then nextPart = Mail.Length + 4
       If nextPart-4 > index Then
         HandlePart(headers.ToString, Mail.SubString2(index, nextPart-4), Msg)
       End If
       index = nextPart
       ReadNextLine(Mail)
       headers.Initialize
     End If
   Loop
End Sub
 
Upvote 0

bryon

Member
Licensed User
Longtime User
With this code it is still cutting off two bytes at the end of the file.
 

Attachments

  • Capture.PNG
    Capture.PNG
    74.8 KB · Views: 146
Upvote 0
Top