Android Question validbase64

Masoud44

Member
Hi
I have a question
ValidBase64 with "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3 }=|[A-Za-z0-9+/]{2}==)$" does not respond for large bitmap strings and causes the app to crash,
How to solve it to check valid of large file?
 

MicroDrie

Well-Known Member
Licensed User
Longtime User
A RegEx is not as fast as testing whether a given Base64 string is converted without error. The program below tests via the inline Java object method whether the conversion runs error-free with the return value true and produces a return value false with an incorrect Base64 string.

Quick Base64 string test:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    
    Private NativeMe As JavaObject
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
#iF B4A
    If FirstTime Then
        NativeMe.InitializeContext
    End If
#End If

#IF b4J
    NativeMe = Me
#End If

    Dim s As String = NativeMe.RunMethod("checkBase64", Array As String("YmFzZTY0IGRlY29kZXI="))
    Log(s)

    Dim s As String = NativeMe.RunMethod("checkBase64", Array As String("falseYmFzZTY0IGRlY29kZXI="))
    Log(s)
    
    Dim s As String = NativeMe.RunMethod("FirstMethod", Null)
    Log(s) 'will print Hello World!
    
End Sub

#If JAVA
import java.util.Base64;
    public static boolean checkBase64(String s64) {
        Base64.Decoder decoder = Base64.getDecoder();
        try {
            decoder.decode(s64);
        } catch(IllegalArgumentException iae) {
            // That string wasn't valid.
            return false;
        }
        return true;
    }
    
    public static String FirstMethod() { //<-- static method
    return "Hello world!";
}

#End If
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
does not respond for large bitmap strings and causes the app to crash,
Without a small example program, you remain guessing as to what the cause is. The most obvious cause is that the Base64 is (or has been) longer than a used buffer. As a result, the length of the Base64 string is not a multiple of 3 and/or is incomplete.

I recommend that you first check the correctness of the Base64 file with the program given in the previous post. What is the result?
 
Upvote 0

Masoud44

Member
Thank you ...
I will definitely check and let you know the result as soon as possible
 
Upvote 0

Masoud44

Member
I checked it and got this error:
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
...
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
This result means that the content of the supplied Base64 string is incorrect. Can you post a simple example program showing how you obtained the base64 file?
 
Upvote 0

Masoud44

Member
This result means that the content of the supplied Base64 string is incorrect. Can you post a simple example program showing how you obtained the base64 file?
It won't let me attach it !
it says "the uploaded file is too large!" (my zip file is 814 KB)
It does not allow uploading even 581 KB
 
Upvote 0

Masoud44

Member
This result means that the content of the supplied Base64 string is incorrect. Can you post a simple example program showing how you obtained the base64 file?
2 simple example source code:
sample one:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
End Sub

Sub Globals
    Private NativeMe As JavaObject
    Private Button1 As B4XView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    If FirstTime Then
        NativeMe.InitializeContext
    End If
    File.Copy(File.DirAssets, "img1.jpg", File.DirInternal,"img1.jpg")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub Button1_Click
    Dim s As String = NativeMe.RunMethod("FirstMethod", Null)
    Log(s)
    Dim base0 As String = Base64EncodeDecodeImage.Base64ImageToString(File.DirInternal,"img1.jpg")
    Dim s As String = NativeMe.RunMethod("checkBase64", Array As String(base0))
    Log(s)
End Sub

#If JAVA
import java.util.Base64;
    public static boolean checkBase64(String s64) {
        Base64.Decoder decoder = Base64.getDecoder();
        try {
            decoder.decode(s64);
        } catch(IllegalArgumentException iae) {
            // That string wasn't valid.
            return false;
        }
        return true;
    }
    
    public static String FirstMethod() { //<-- static method
    return "Hello world!";
}

#End If
B4X:

sample two main code:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

'#BridgeLogger: True

Sub Process_Globals
    Public ActionBarHomeClicked As Boolean
    Public NativeMe As JavaObject
End Sub

Sub Globals
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim pm As B4XPagesManager
    pm.Initialize(Activity)
    If FirstTime Then
        NativeMe.InitializeContext
    End If
    
End Sub

#Region Delegates

Sub Activity_ActionBarHomeClick
    ActionBarHomeClicked = True
    B4XPages.Delegate.Activity_ActionBarHomeClick
    ActionBarHomeClicked = False
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
    Return B4XPages.Delegate.Activity_KeyPress(KeyCode)
End Sub

Sub Activity_Resume
    B4XPages.Delegate.Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    B4XPages.Delegate.Activity_Pause
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    B4XPages.Delegate.Activity_PermissionResult(Permission, Result)
End Sub

Sub Create_Menu (Menu As Object)
    B4XPages.Delegate.Create_Menu(Menu)
End Sub

#if Java
public boolean _onCreateOptionsMenu(android.view.Menu menu) {
     processBA.raiseEvent(null, "create_menu", menu);
     return true;
    
}
#End If
#End Region

#If JAVA
import java.util.Base64;
    public static boolean checkBase64(String s64) {
        Base64.Decoder decoder = Base64.getDecoder();
        try {
            decoder.decode(s64);
        } catch(IllegalArgumentException iae) {
            // That string wasn't valid.
            return false;
        }
        return true;
    }
    
    public static String FirstMethod() { //<-- static method
    return "Hello world!";
}

#End If

sample two B4xMainPage:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

Private Sub Button1_Click
 
    Dim s As String = Main.NativeMe.RunMethod("FirstMethod", Null)
    Log(s) 'will print Hello World!
    
    Dim s As String =Main.NativeMe.RunMethod("checkBase64", Array As String("YmFzZTY0IGRlY29kZXI="))
    Log(s)

    Dim s As String =Main.NativeMe.RunMethod("checkBase64", Array As String("falseYmFzZTY0IGRlY29kZXI="))
    Log(s)
  
End Sub
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Hi
I have a question
ValidBase64 with "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3 }=|[A-Za-z0-9+/]{2}==)$" does not respond for large bitmap strings and causes the app to crash,
How to solve it to check valid of large file?
if you have a "large" string you can use ( compatible B4X --> B4J - B4A - B4i )

B4X:
Sub ValidBase64(text As String) As Boolean
    Dim chunkSize As Int = 100000 ' size block
    Dim textLength As Int = text.Length
    Dim startIndex As Int = 0
    Dim isMatch As Boolean = True

    Do While startIndex < textLength
        Dim endIndex As Int = Min(startIndex + chunkSize, textLength)
        Dim textChunk As String = text.SubString2(startIndex, endIndex)
  
        If Regex.IsMatch("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$", textChunk.Trim) = False Then
            isMatch = False
            Return False
        End If
  
        startIndex = endIndex
    Loop
 
    If isMatch Then
        Return True
    Else
        Return False
    End If
End Sub


Dim check_base64 As Boolean = ValidBase64(base)

'Check if Valid Base64
If check_base64 = True Then
       'Ok...is valid
  Else
        Log("Error")
End If
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…