B4A Library [B4X] Library Base64 encode/decode Image and File library

TILogistic

Expert
Licensed User
Longtime User
Very good:

I had separate classes for B4A, B4J and B4I ..

Can I take your project and add other routines?
 

LucaMs

Expert
Licensed User
Longtime User
There is an error in the manifest file (how come no one noticed? Is maybe my file corrupted? ?. I don't think so; you (all) should use more often B4J )

B4X:
Version=1.10
B4J.DependsOn=jStringUtils, jXUI, jiBitmapCreator, jRandomAccessFile

jiBitmapCreator - it should be jBitmapCreator, of course.
 
Last edited:

MarcoRome

Expert
Licensed User
Longtime User
BugFix is released #1
 

MarcoRome

Expert
Licensed User
Longtime User
In attachment #1 you have Rel.1.13
Add new methods BaseAnyFileToString + Base64StringToAnyFile. To convert any file to Base64 & any file in Base64 to file
Retried with all three environments (B4A + B4i + B4J)
 

hayk

Member
Hi everyone I am facing following problem:
method 1 result: works correct and returns correct image.
2:
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString(File.DirInternal,"653.jpg")



method 2 result: NOT correct and returns incorrect image. I have used B4XBitmap and Bitmap. Result is the same.
1:
        Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2(bmp1)

.

Where I made mistake? I am using library v1.13
 

MarcoRome

Expert
Licensed User
Longtime User
Pls, attach an example
 

hayk

Member
Pls, attach an example
Thanks for response!
Here is an example

main:
        Dim bmp1 As Bitmap
        bmp1.Initialize(File.DirInternal, "123456.jpg")
        Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2(bmp1)
        Sleep(5)
        If Base64EncodeDecodeImage.ValidBase64(base) Then
            ImageView1.Bitmap = Base64EncodeDecodeImage.Base64StringToImage(base)
        End If
result is the same by using B4XBitmap also
 
Last edited:

MarcoRome

Expert
Licensed User
Longtime User

Yes you are right.
As soon as I have time I will make the change.
 

MarcoRome

Expert
Licensed User
Longtime User
In version 1.14 you have a bugfix and added:

Base64ImageToString2Type
Do not use Base64ImageToString2 but use Base64ImageToString2Type as follows:
B4X:
'If file is JPG or JPEG
Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "JPG")
'If file in PNG
Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "PNG")

ValidBase64
Modified the ValidBase64 function which was giving an error on very large files method.

To convert any file to Base64.

The library is for B4X (Tested with B4J, B4A, B4i)

The Example Code:

B4X:
'File 653 kb
    File.Copy(File.DirAssets, "653.jpg", File.DirInternal,"653.jpg")
    File.Copy(File.DirAssets, "375.png", File.DirInternal,"375.png")
    File.Copy(File.DirAssets, "26.jpeg", File.DirInternal,"26.jpeg")
    Sleep(300)
   
'    'Example 1
'    'Image to Encode64
'    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString(File.DirInternal,"653.jpg")
'  
'    Log(base)
'    'Check if Valid Base64
'    If Base64EncodeDecodeImage.ValidBase64(base) Then
'        ImageView1.Bitmap = Base64EncodeDecodeImage.Base64StringToImage(base)
'    End If


    'Example 2
    'B4XBitmap
    Dim bmp1 As B4XBitmap
    bmp1 = LoadBitmap(File.DirInternal, "26.jpeg")
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "JPG")
   
    'bmp1 = LoadBitmap(File.DirInternal, "653.jpg")
    'Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "JPG")
   
    'bmp1 = LoadBitmap(File.DirInternal, "375.png")
    'Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "PNG")
   
    'Bitmap
'    Dim bmp1 As Bitmap
'    bmp1.Initialize(File.DirInternal, "653.jpg")
'    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "JPG")
   
    Log(base.Length)
    Dim check_base64 As Boolean = Base64EncodeDecodeImage.ValidBase64(base)

    'Check if Valid Base64
    If check_base64 = True Then
        ImageView1.Bitmap = Base64EncodeDecodeImage.Base64StringToImage(base)
    Else
        Log("Error")
    End If
   
   

   
'    'FILE PDF
'  
'    'File 653 kb
'    'File.Copy(File.DirAssets, "example_5_pages.pdf", File.DirInternal,"example_5_pages.pdf")
'    File.Copy(File.DirAssets, "example_5_pages.pdf", Starter.Provider.SharedFolder, "example_5_pages.pdf")
'    Sleep(300)
'  
'  
'    Dim converti_base64 As String = Base64EncodeDecodeImage.Base64AnyFileToString(Starter.Provider.SharedFolder, "example_5_pages.pdf")
'  
'    Log(converti_base64.Length)
'    Dim check_base64 As Boolean = Base64EncodeDecodeImage.ValidBase64(converti_base64)
'
'    'Check if Valid Base64
'    If check_base64 = True Then
'        Base64EncodeDecodeImage.Base64StringToAnyFile(converti_base64, Starter.Provider.SharedFolder, "convert.pdf")
'    End If
'  
'    Sleep(200)
'  
'    Dim Intent1 As Intent
'
'    Intent1.Initialize(Intent1.ACTION_VIEW, "")
'    'Vedi Manifest per ShareFolder
'    Starter.Provider.SetFileUriAsIntentData(Intent1, "convert.pdf")
'    Intent1.SetComponent("android/com.android.internal.app.ResolverActivity")
'    Intent1.SetType("application/pdf")
'
'    StartActivity(Intent1)

In attachment library b4xlib 1.14
 

Attachments

  • Base64EncodeDecodeImage.b4xlib
    1.9 KB · Views: 106

b4x-de

Active Member
Licensed User
Longtime User
Thank you for this solution! I have one question: Is the second argument of Base64EncodeDecodeImage() the type of the target or the the of the source image in B4Xbitmap?
 

hayk

Member

Thank you very much! ?
 

MarcoRome

Expert
Licensed User
Longtime User
Thank you for this solution! I have one question: Is the second argument of Base64EncodeDecodeImage() the type of the target or the the of the source image in B4Xbitmap?
Source.
if it's a png it will remain a png if it's a jpg it will remain a jpg

B4X:
    'File source JPG / JPEG
    Dim bmp1 As B4XBitmap
    bmp1 = LoadBitmap(File.DirInternal, "26.jpeg")
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "JPG")
   
    Dim bmp1 As B4XBitmap
    bmp1 = LoadBitmap(File.DirInternal, "267.jpg")
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "JPG")
   
    'File source PNG
    Dim bmp1 As B4XBitmap
    bmp1 = LoadBitmap(File.DirInternal, "2678.png")
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "PNG")
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…