Android Question Error Loading Bitmap

Carlos Muniz

Member
Licensed User
Longtime User
Hello:

Big troubles here. I am trying to load a bit map with a BASE64 string, but the whole thing doesn't work.

Thanx in advance.

Regards.

B4X:
Private FIY as String
FIY = "abcdefghijklmnopqrstuvwxyz01234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"


    Sub Button3_Click
    Dim b As Bitmap
    Dim bufferX() As Byte
    Dim In As InputStream
    Dim su As StringUtils
    Dim bc As ByteConverter

    Dim text As String = FIY
    FIY1 = su.EncodeBase64(bc.StringToBytes(text,"utf-8"))
    FIY1 = "/9j/" & FIY1

    bufferX = su.DecodeBase64(FIY1)
    In.InitializeFromBytesArray(bufferX, 0, bufferX.Length)
    b.Initialize2(In)
    ImageView1.Bitmap = b
    ImageView1.Gravity = Gravity.fill

End Sub


Error Message:


main_button3_click (java line: 565)
java.lang.RuntimeException: Error loading bitmap.
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:539)
at b4a.example.main._button3_click(main.java:565)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:7869)
at android.widget.TextView.performClick(TextView.java:14958)
at android.view.View.performClickInternal(View.java:7838)
at android.view.View.access$3600(View.java:886)
at android.view.View$PerformClick.run(View.java:29362)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8019)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
 
Last edited:

drgottjr

Expert
Licensed User
Longtime User
B4X:
    dim encodedstring as string = " ...... "      ' we assume this is a base64 encoded string that represents a bitmap
    Dim bytes() As Byte = su.DecodeBase64( encodedstring )

    Dim In As InputStream
    In.InitializeFromBytesArray(bytes, 0, bytes.Length)
    Dim bmp As Bitmap
    bitmap.Initialize2(In)


    Dim imageview As ImageView
    imageview.Initialize("iv")
    Activity.AddView(imageview,0%x,0%y,100%x,100%y)
    imageview.Bitmap = bitmap
    imageview.Gravity = Gravity.FILL

if your base64 string FIY represents an image, just convert to byte array. i don't see what you're trying to achieve with those extra steps of yours. if your string represents a jpg, the jpg header is already there. look at the first 8 bytes of your base64 string. the jpg header should be there. if it's not, then it's not a jpg. you can't just prepend some random header and think you've made a jpg.

i converted a base64 string to a bitmap using the above code and displayed it in an image view without a problem. the jpg head was in the base64 string ("/9j/4AAQ").
 
Upvote 0

Carlos Muniz

Member
Licensed User
Longtime User
Hello Drgottjr !

I am trying to transform any string into a BASE64 and charge an bitmap with it. The mais problem is that as I suppose, BASE64 string has any control character, something like a CRC check. I tryied to change just one character in the BASE64 string and... disaster in the bitmap charge.
My main problem is to download images in a string shape, in 1460 bytes packs. I would like to download images in string format and transform them to BASE64, inside the App. If a try to download images in BASE64 format, and I loose just one bit the whole thing crashes. I believe, downloading in string form, the image will be distorted, but the App will not crashes.
Another thing: Where can I find Base64 string structure? The head, the complete string... I have a string like this

"data:image/jpeg;base64,/9j/4AAQSkZJRgABA

but I can't understand the meaning of each character and why is it dangerous to change one character, at least ?

Thanks a lot.
 
Upvote 0

Carlos Muniz

Member
Licensed User
Longtime User
Hi:

Your App works fine. But, the string must be downloaded in BASE64 format, completelly critical. Any problem in the transmmition and the App crashes. I want receive any string and transform it in BASE64, doesn't metter if the picture will be distorted. The goal is the App stability. BASE64 string is completely unstable, dangerous...

Thanks once more.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
sorry to say that almost everything you've said is incorrect. i'm not sure where to begin.

there is no base64 "structure". there are some rules, but they are not complicated. the purpose is to convert 8-bit binary characters to 7-bit characters for transport over the internet. many protocols (eg, email) still use base64. you can google the formula for base64 coding/encoding.

you can't look at a base64 string and understand it. there are no "blocks" in a base64 string. it's just a "string". when you convert a base64 string back to binary bytes, the result may well have a block structure (eg, image files), but this has nothing to do with base64. it is possible to look at the first few characters of a base64 string and guess what the string represents. for example, one of the identifying signatures of a jpg file is "/9j/" at the start. you can see that in a base64 string or in a .jpg file (with a binary viewer). this doesn't mean you can take a base64 string and put "/9j/" at the start and make a .jpg out of it.

B4X:
"data:image/jpeg;base64,/9j/4AAQSkZJRgABA
is not a base64 string. the part that starts with "/9j/" is the start of the base64 string.
B4X:
data:image/jpeg;base64
is just a regular string that tells a browser that a base64-encoded image follows.

there is no reason why you cannot download a base64 string. a server simply has to serve you one. if your server does not serve images encoded as base64, you can't do it. you are, of course, free to download an image as an image and convert it to base64 if you like. although i do not understand why you would want to do that.

B4X:
I am trying to transform any string into a BASE64 and charge an bitmap with it.
if you are talking about taking a base64-encoded string that represents a bitmap and you want to see it as an image, then, yes, this is easy.
if, on the other hand, you are talking about literally taking any string and making it into an image, then you will need a library (at least), and base64 is probably the least of your concerns. are you saying: "hello, carlos" ----> base64 ----> bitmap ? this is something quite different. of course, it be done, but you have to know the formats of image files and how to build one. these formats will not be base64.

you have to know how to create a jpg file from nothing. "hello, carlos" is just a string of bytes. if you know how to create a jpg file, you would know how to insert that string of bytes into the format.

if you want to take "any" string and make it into a bitmap with b4a, it's simple, and you don't need base64: create a bitmap, put a canvas on top, draw "hellow, carlos" on the canvas and write the canvas.bitmap to a file. no base64 needed.

if you're talking about downloading things like "data:image/jpeg;base64,/9j/4AAQSkZJRgABA[" from some website, then i've explained what that string is telling you. take the base64 part and do what i did in my example.
 
Upvote 0

Carlos Muniz

Member
Licensed User
Longtime User
Sorry to say that almost everything you've said is unuseful.

The BASE64 has an structure, of course. Pay attention to learn it. Is there a prefix defining the kinf of object it represents. BASE64 turns an 8 bit structure into an 6 bits structure (64 characteres). 52 letters, 10 numbers and 2 control characteres. 6 bits, not 7 bits as you wrongly said in your philosophical answer.

Please, learn one thing: in computer world, answers must be direct, right to the arrow. To do it proceed this way, to do that proceed in other way. Directly, not philosophy.
And don't forget to point the libraries. Did you learn or may I draw ? Next time do this way or give me your silence...
 
Last edited:
Upvote 0
Top