Android Question Decoding Base64 To Image

carloz

Member
Licensed User
Longtime User
hello All,

have been trying to save a image from a base64 string.. the below code works perfectly

B4X:
Sub ImageUri(uri As String) As Bitmap
   Dim s As String
   Dim n As Int
   n = uri.IndexOf(",")
   Log("n is " & n)
   If n < 0 Then
      s = uri
   Else
      s = uri.SubString(n+1)
   End If
   Dim su As StringUtils
    Dim bytes() As Byte = su.DecodeBase64(s)
   
    Dim In As InputStream
   In.InitializeFromBytesArray(bytes, 0, bytes.Length)
    Dim bmp As Bitmap
    bmp.Initialize2(In)
    In.Close
    Return bmp
End Sub
as long as the input string is of type "data:image/png;base64"

but fails if the input string is of type "data:image/svg+xml;base64" :(

Can we not decode svg/xml with stringutils? What am i missing here?

regards
carloz
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It parses it correctly:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim s As String = File.ReadString(File.DirAssets, "svg_xml_base64strings.txt")
   Dim m As Matcher = Regex.Matcher($",([^"]+)""$, s)
   Dim su As StringUtils
   Do While m.Find
     Dim data() As Byte = su.DecodeBase64(m.Group(1))
     Log($"data length: ${data.Length}"$)
   Loop
End Sub
 
Upvote 0

carloz

Member
Licensed User
Longtime User
hi erel,

i get the following errors while reading the string and outputting a bitmap using the code in post #1 at "
bmp.Initialize2(In)"
maybe the base64 string is incorrect?

B4X:
LogCat connected to: B4A-Bridge: XOLO Play8X-1100
--------- beginning of /dev/log/main
n is 25
Error occurred on line: 40 (main)
java.lang.RuntimeException: Error loading bitmap.
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:521)
    at com.pramod.webviewtest.main._imageuri(main.java:460)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:77)
    at android.view.View.performClick(View.java:4463)
    at android.view.View$PerformClick.run(View.java:18770)
    at android.os.Handler.handleCallback(Handler.java:808)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:5292)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
    at dalvik.system.NativeStart.main(Native Method)
--------- beginning of /dev/log/system
** Activity (main) Resume **
 
Upvote 0
Top