Android Question Problem with B4XBitmap and image as array

Status
Not open for further replies.

fred-han

Member
Licensed User
Hi,
i am new with B4A and try to store a bitmap into a sqlite table.
The storage code seems to work corrrectly.

>>
Dim b() As Byte = ImageToBytes(xui.LoadBitmap(File.DirAssets, "bmw_bild_icon.png"))
SQL_1.ExecNonQuery2("INSERT INTO kfz VALUES (1, 'BMW 316 i compact')", Array As Object(b,1))
<<<


But when i try to get the image from the db, an error occurs .

>>
Dim puffer As Byte = Cursor.GetBlob("image")
Dim bild As B4XBitmap = BytesToImage(puffer) ---> Error: Types does not match


img_1.Background = bild
<<<

I use the given Sub:

>>>
Public Sub BytesToImage(bytes() As Byte) As B4XBitmap
Dim In As InputStream
In.InitializeFromBytesArray(bytes, 0, bytes.Length)
#if B4A or B4i
Dim bmp As Bitmap
bmp.Initialize2(In)
#else
Dim bmp As Image
bmp.Initialize2(In)
#end if
Return bmp
End Sub
<<<

What is wrong??

thanks in advance..
 

DonManfred

Expert
Licensed User
Longtime User
Dim puffer As Byte = Cursor.GetBlob("image")
You are defining a single Byte but you want to fill it with an Array of Bytes.

Change the Dim-Line to
B4X:
Dim puffer() As Byte = Cursor.GetBlob("image")
 
Last edited:
Upvote 0

Guenter Becker

Active Member
Licensed User
Hello to you,
just read this posts and follwed the solution. I have a Blob column in sqlite 3 Database and inserted a bitmap. Like this:

My Code:
Sub Button1_Click
    ImageView1.Bitmap = LoadBitmap(File.DirAssets,"4848Achtung.bmp")
    
    Dim m As List
    m.Initialize
    m.add(ImageView1.bitmap)
    m.add(EditText1.Text)
    
    Dim s As String = "INSERT INTO Test (BLOBcolumn,Textcolumn) VALUES(?,?)"
    
    Starter.sql1.Initialize(File.DirInternal,"test.db",False)
    'If Starter.sql1.IsInitialized Then
    '    Starter.sql1.ExecNonQuery2(s,m)
    'End If
    
    EditText1.text = ""
    ImageView1.Bitmap = Null
    
    Public buffer() As Byte 'declare an empty byte array

    Dim r As ResultSet = Starter.sql1.ExecQuery("SELECT * from Test WHERE Textcolumn = 'abc'")

        
    If r.RowCount > 0 Then
        r.Position = 0
        EditText1.Text = r.Getstring("Textcolumn")
        
    
        buffer = r.GetBlob("BLOBcolumn")

        ImageView1.Bitmap=BytesToImage(buffer)
    End If
End Sub

Public Sub BytesToImage(bytes() As Byte) As B4XBitmap ' also tried bitmap
    Dim In As InputStream
    In.InitializeFromBytesArray(bytes, 0, bytes.Length)

    Dim bmp As Bitmap
    bmp.Initialize2(In)

    Return bmp
End Sub

Checked that buffer is filled after executing sql.

Debug at line bmp.Initialize.. it raises error as followes:

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 78 (Main)
java.lang.RuntimeException: Error loading bitmap.
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:539)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
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:858)

Can anyone give an idea to me whats worng?
Best regards Guenter
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
1. You should NEVER post to existing threads! Always create a new thread for any Issue you have.

2.
m.add(ImageView1.bitmap)
This will not work. You are adding an Object where you should add an Bytearrray.

Convert the bitmap to a bytearray first and add the array to the list.
 
Upvote 0
Status
Not open for further replies.
Top