Android Question Bitmap Initialize2 problem

Carlos Muniz

Member
Licensed User
Longtime User
Hello:

I am trying to transform byte array in bitmap. The bitmap Initialize2 command never works. Where is my mistake ?

Public bytesFOTO() as Byte

......

Public Sub BytesToImage2()
Dim In As InputStream
Dim bmp As Bitmap

In.InitializeFromBytesArray(bytesFOTO, 0, bytesFOTO.Length)
bmp.Initialize2(In)

End Sub


Thanx for any help.
 

drgottjr

Expert
Licensed User
Longtime User
what do you mean by it never works? is there an error? what are you expecting to happen? where is bytesFOTO() declared? where is it populated? you don't actually do anything with bmp, so how do you know there's a problem?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Please use [CODE]code here...[/CODE] tags when posting code.

codetag001.png

codetag002.png

codetag003.png
 
Upvote 0

Carlos Muniz

Member
Licensed User
Longtime User
Hello:

I gonna start answering:

Never works means the App crashes. But I discovered a detail: InputStream must be a Base64. I discovered it, investigating, not studying, it is amazing. The bitmap documentation doesn't point Base64. Now, I am looking for a function to transform a string in a Base64 encoded string.
bytesFOTO is declared inside Globals. It is not populated. As soon as I get it I try to initialize a bitmap.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
if the app crashes, there will be an error in the log. you need to include that for us.
if the bytes array is never populated (with bytes), how can you expect a bitmap to be created?

also, just a suggestion, if you're going to make a little sub to do your conversion, you shouldn't use a global (bytesFOTO) in it. the idea is to pass a bytes array as a parameter to the sub. that way, the sub can be used as part of a library of little routines that you have. otherwise, the only way it works in other apps is if you always declare bytesFOTO as a global in every app you write that wants to use the sub.

eg:
B4X:
Public Sub BytesToImage2( bytes as byte()) as bitmap
   if bytes.length = 0 then
      return null
   end if

   Dim In As InputStream
   Dim bmp As Bitmap

   In.InitializeFromBytesArray(bytes, 0, bytes.Length)
   bmp.Initialize2(In)
   return bitmap
End Sub

this way, the sub doesn't have to know the name of the byte array you want converted to a bitmap. by using a global variable, if you had 4 byte arrays to convert to bitmaps, they would all have to be called bytesFOTO because that's what your sub is expecting. by passing a parameter, you just pass each byte array to the sub:

B4X:
bim b1(), b2(), b3() b4() as byte
'  fill each with bytes
'then:
dim bitmap1 as bitmap = bytestoimage2( b1 )
dim bitmap2 as bitmap = bytestoimage2( b2 )
dim bitmap3 as bitmap = bytestoimage2( b3 )
dim bitmap4 as bitmap = bytestoimage2( b4 )
(
 
Upvote 0

Carlos Muniz

Member
Licensed User
Longtime User
My mistake.... the bytes array is populated, of course. It is not saved, as the goal is to transform it into a bitmap, at once.
Another problem: the string that generates bytesFOTO is not in Base64 structure. How can I do that ? Any string to a Base64 string.

Thanx a lot.
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
sorry, you said
It is not populated
. i understood that to mean it was not populated.

that still leaves the matter of the crash. what is the error thrown when the app crashes? how exactly are you populating the array? where are the bytes coming from?
 
Upvote 0

Carlos Muniz

Member
Licensed User
Longtime User
There is nothing talking about a Base64 string in the bitmap environment. Nothing. You must to become a wizard to find the information all around.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
InputStream must be a Base64
No. What makes you think it needs to be?

Edit: plus you never posted the error message, leaving us guessing as to what is going on
 
Upvote 0

Carlos Muniz

Member
Licensed User
Longtime User
Hi:

Follows the error message:

main_auxiliar_foto (java line: 770)
java.lang.RuntimeException: Error loading bitmap.
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:539)
at b4a.example.main._auxiliar_foto(main.java:770)
at b4a.example.main._recebeu(main.java:2637)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1083)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:1038)
at b4a.example.starter._astream_newdata(starter.java:201)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.BA$2.run(BA.java:370)
at android.os.Handler.handleCallback(Handler.java:883)

and the app lines:

B4X:
    Dim buffer() As Byte
    Dim b As Bitmap
    Dim In As InputStream

    bytesFOTO = FIG_STRING.GetBytes("UTF8")
    FIG_STRING = FIG_STRING.SubString(FIG_STRING.IndexOf(",")+1)
    buffer = su.DecodeBase64(FIG_STRING)
    In.InitializeFromBytesArray(buffer, 0, buffer.Length)
    File.WriteString(File.DirInternal, IndiceDeImagens & ".txt", FIG_STRING)
    b.Initialize2(In)
    ImageView1xx.Bitmap = b
    ImageView1xx.Gravity = Gravity.center

bytesFOTO and FIG_STRING are defined at GLOBALS. IndiceDeImagens contains the picture name without ".txt".
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0
Top