Android Question String to Bitmap image conversion

GaNdAlF89

Active Member
Licensed User
Longtime User
Hi, I use a web service, and SOAP to communicate with it. There is a specific function that give me an image in string format.
I wrote the code below:
B4X:
Dim byteConv As ByteConverter
Dim imageBuffer() As Byte
imageBuffer = byteConv.StringToBytes(Logo,"UTF-8") 'Logo is the string image
Dim i_s As InputStream
i_s.InitializeFromBytesArray(imageBuffer,0,imageBuffer.Length)

Dim bmp As Bitmap
bmp.Initialize2(i_s)
i_s.Close
but on the line:
B4X:
bmp.Initialize2(i_s)
I take this exception:
B4X:
bmp.Initialize2(i_s)
java.lang.RuntimeException: Error loading bitmap.
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:521)
    at it.cbsita.pos.app.menu._create_relativelayout(menu.java:730)
    at it.cbsita.pos.app.menu._set_layout(menu.java:1601)
    at it.cbsita.pos.app.menu._activity_create(menu.java:269)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at it.cbsita.pos.app.menu.afterFirstLayout(menu.java:89)
    at it.cbsita.pos.app.menu.access$100(menu.java:16)
    at it.cbsita.pos.app.menu$WaitForLayout.run(menu.java:74)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:155)
    at android.app.ActivityThread.main(ActivityThread.java:5696)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
java.lang.RuntimeException: Error loading bitmap.

Where is the problem? Thanks a lot.
 

KMatle

Expert
Licensed User
Longtime User
Most likely the imagestring ("logo") is Base64 encoded. Example (B4J) to decode it:

B4X:
FileString64=ItemMap.Get("itemimg")
Dim FileBytes() As Byte
Dim su As StringUtils

FileBytes=su.DecodeBase64(FileString64)
                      
Dim img As Image
                      
Dim Inputstream1 As InputStream
Inputstream1.InitializeFromBytesArray(FileBytes,0,FileBytes.Length)
img.Initialize2(Inputstream1)
Inputstream1.Close  
                      
Dim iv As ImageView
iv.Initialize("")
iv.SetImage(img)
 
Upvote 0

GaNdAlF89

Active Member
Licensed User
Longtime User
Most likely the imagestring ("logo") is Base64 encoded. Example (B4J) to decode it:

B4X:
FileString64=ItemMap.Get("itemimg")
Dim FileBytes() As Byte
Dim su As StringUtils

FileBytes=su.DecodeBase64(FileString64)
                     
Dim img As Image
                     
Dim Inputstream1 As InputStream
Inputstream1.InitializeFromBytesArray(FileBytes,0,FileBytes.Length)
img.Initialize2(Inputstream1)
Inputstream1.Close 
                     
Dim iv As ImageView
iv.Initialize("")
iv.SetImage(img)
Thanks a lot! Your suggestion has solved my problem! ;-)
 
Upvote 0
Top