Android Question StringToBitmap Error

Masoud44

Member
Hi
when i use of BitmapToString(bitmap1) and StringToBitmap(base) into imageview it work corctly
but when insert it string to mysql field and read it have an error on command img.Initialize2(in)
java.lang.RuntimeException: Error loading bitmap.

B4X:
Sub JobDone (job As HttpJob)

    If job.Success=True Then
        ProgressDialogHide
        Select job.JobName
            Case "write"
                MsgboxAsync(job.GetString,"Come")
            Case "read"
                Log("read")
                Dim res As String
                res=job.GetString
                Log("Response from server: " & res)
                If res<>0 Then
                    Dim parser As JSONParser
                    parser.Initialize(res)
                    Dim ListOfpersons As List
                    ListOfpersons = parser.NextArray
                                
                    If ListOfpersons.Size=0 Then
                        Log("not found id : "&id)
                    Else
                            Dim person As Map
                            Dim txtpic As String
                            person = ListOfpersons.Get(0)
                            txtname.Text = person.Get("name")
                            txtpic = person.Get("pic")
                            ImageView3.Bitmap=StringToBitmap(txtpic)
                    End If
                End If
        End Select
    Else
        ToastMessageShow("Error During Connecting",False)
    End If
    
End Sub

B4X:
Private Sub Breadsql_Click
            
    Dim data As String

    data="&id="&id
 
    ht.Initialize("read",Me)
    ht.PostString("http://" & ServerIP & "/p/readimage.php",data)
    ProgressDialogShow("Please Wait ...")
End Sub

B4X:
Private Sub StringToBitmap(s As String) As B4XBitmap
    Dim in As InputStream
    Dim su As StringUtils
    Dim b() As Byte = su.DecodeBase64(s)
    in.InitializeFromBytesArray(b, 0, b.Length)
    #if B4J
    Dim img As Image
    #else
    Dim img As Bitmap
    #end if
    img.Initialize2(in)
    Return img
End Sub

java.lang.RuntimeException: Error loading bitmap at line img.Initialize2(in) : java.lang.RuntimeException: Error loading bitmap.

The pic2.txt file is the content of the image text field
 

Attachments

  • pic2.txt
    160.9 KB · Views: 119

Brian Dean

Well-Known Member
Licensed User
Longtime User
When I try to convert your code sample using slightly different B4J code I get this error message ...
java.io.IOException: Bad Base64 input character decimal 92 in array position 62
So it looks to me that the string that you have retrieved from your database is not the string that you expected. The code sample that you provided is probably not where the error lies.

For what its worth, the Base64-to-image code that I used is ...
B4J code extract:
Sub Base64StringToImage(s As String) As Image
    Dim su As StringUtils
    Dim bytes() As Byte = su.DecodeBase64(s)
    Dim str As InputStream
    str.InitializeFromBytesArray(bytes, 0, bytes.Length)
    Dim bmp As Image
    bmp.Initialize2(str)
    str.Close
    Return bmp
End Sub
 
Upvote 0

Masoud44

Member
to use of StringToBitmap
you must be used
BitmapToString for save image to mysql field.
mysql field type is longtext

B4X:
Private Sub BitmapToString(bmp As B4XBitmap) As String
    Dim out As OutputStream
    out.InitializeToBytesArray(1000)
    bmp.WriteToStream(out, 100, "PNG")
    out.Close
    Dim su As StringUtils
    Return su.EncodeBase64(out.ToBytesArray)
End Sub

Private Sub StringToBitmap(s As String) As B4XBitmap
    Dim in As InputStream
    Dim su As StringUtils
    Dim b() As Byte = su.DecodeBase64(s)
    in.InitializeFromBytesArray(b, 0, b.Length)
    #if B4J
    Dim img As Image
    #else
    Dim img As Bitmap
    #end if
    img.Initialize2(in)
    Return img
End Sub
 
Upvote 0
Top