Android Question problem with Base64ImageToString2

Masoud44

Member
Hi everyone
I have a problem when use this code

B4X:
    'Convert without Dir / File as request #8
    Dim bitmap1 As B4XBitmap = ImageView1.Bitmap
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2(bitmap1)
    ImageView2.Bitmap = Base64EncodeDecodeImage.Base64StringToImage(base)

Why the color of the image changes to black and white ?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use these two methods:
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

1. Depends on StringUtils
2. It is cross platform
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Masoud44

Member
Hi everyone
I have a problem when use this code

B4X:
    'Convert without Dir / File as request #8
    Dim bitmap1 As B4XBitmap = ImageView1.Bitmap
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2(bitmap1)
    ImageView2.Bitmap = Base64EncodeDecodeImage.Base64StringToImage(base)

Why the color of the image changes to black and white ?

what does this method do?
To convert the image inside the ImageView.bitmap to a string
 

Attachments

  • 1.png
    1.png
    224.2 KB · Views: 80
Upvote 0

Masoud44

Member
You can use these two methods:
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

1. Depends on StringUtils
2. It is cross platform
Hi Erel
when i use of BitmapToString(bitmap1) and StringToBitmap(base) into imageview corctly work
but when insert it string to mysql field and read it have a error on command img.Initialize2(in)
java.lang.RuntimeException: Error loading bitmap.
 

Attachments

  • 2.png
    2.png
    48.3 KB · Views: 74
Upvote 0
Top