B4J Question Fill B4XBitmap from base-encoded image

TheRealMatze

Active Member
Licensed User
Hey!
I want to hard-code icons into my app. I need it as B4XBitmap. Filling the object from file with
B4X:
Dim bmp As B4XBitmap=xui.LoadBitmap("d:\folder","icon.png")
works fine, but how can i load a base64-encoded image into this object?

Regards
Matthias
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Dim su As StringUtils
Dim b() As Byte = su.DecodeBase64(Base64String)
Dim bmp As B4XBitmap = BytesToImage(b)
 
Upvote 0

TheRealMatze

Active Member
Licensed User
Ok that sound simple, but I´ve tried with one of my images and it the IDE is mostly unusable with a base64-string inside the function.
The size of the base64 is about 18k, is there any limitation?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
is there any limitation?
You did add the base64 string to your Source? This is the Mistake. Store it in a File and load this file. Or store it in a KVS or a Database.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Maybe something like
B4X:
    Dim su As StringUtils
    Dim tr As TextReader
    tr.Initialize (File.OpenInput("your dir","your base64 file"))
    Dim bmp As B4XBitmap = BytesToImage(su.DecodeBase64(tr.ReadAll))
 
Upvote 0

TheRealMatze

Active Member
Licensed User
But where is the benefit when i load the base64-string from file? In this case i can use my code in the first post...
When i want to use a file, is it correct that i put these in the "files" tab inside the IDE and use "File.DirAssets" as folder? I have currently no iphone here - does it works in b4i also?

Thanks!

PS.: Why are my posts under supervision?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should tell us why you want to use a base64 string for your image. This is a very strange way to load images.
Just put the files in the Files folder.
When i want to use a file, is it correct that i put these in the "files" tab inside the IDE and use "File.DirAssets" as folder?
Yes.
does it works in b4i also?
Yes.

You should never put data inside your code, especially not large data. You will kill the compiler and your app will be unmaintainable.

PS.: Why are my posts under supervision?
You need to become a "licensed member". You can make a contribution of any amount: https://www.b4x.com/b4a.html
 
Upvote 0

TheRealMatze

Active Member
Licensed User
Hi,
tell us why you want to use a base64 string
Just because it´s possible and i dont need to check for files. Before i use a file i need to verify the file is there. Depend on the type of the file i need also a checksum to verify the file i see is the file i expect. When I put it in the code, it's relatively safe, so my thought.
never put data inside your code
Okay, i´ve seen that the enviroment stop responding after insert the base64-String. I did not expect these, because it´s not so big. But ok, if you know it you can handle it...

Back to the main - i try to build a function to load the files...

B4X:
Dim bmp As B4XBitmap=loadBitmapSecure("ImageFile.png")
B4X:
Sub loadBitmapSecure(filename As String) As B4XBitmap
    If File.Exists(File.DirAssets,filename)=True Then
        Return xui.LoadBitmap(File.DirAssets,filename)
    Else
        If(filename.EqualsIgnoreCase("filenotfound.png"))=True Then
            Log("FATAL: fileNotFound Image not found")
            ExitApplication
        End If
        Log("file not found: " & filename)
        Return loadBitmapSecure("fileNotFound.png")
    End If
End Sub

The called file exists, the fileNotFound.png exists - but file.Exists returns false. Why? Is DirAssets a "real" folder, or is it inside the app packed as a kind of "virtual folder"? That would solve the problem by itself...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Top