Android Question Error loading bitmap

dws

Member
Licensed User
Longtime User
Hellp There ... Any help please

1. I have a MySql Database on NAS Disc and i have a table with images ( Blob )
1.1. From PC all image is visible and is ok

Now
2. I have an android app with SQLite3 database
2.1. When syncronize data i read from MySql Database ( from Nas ) and all records i add in SQLite tables
2.1.1. I use mysqllib (v.2.00) for connection to MySql and work correct

The problem is
2.2.1. When read from MySql if i use c.GetBlob("Img") then i get an error
2.2.2. When i use Dim Buffer() as Byte = v.GetString("Img").GetBytes("UTF8") all is ok
2.2.3. Finally save to SQLite using Array as Object(Buffer)

Is 2.2.2. correct method to read an image from MySql Blob field and save to SQLite ???


Thanks
 

dws

Member
Licensed User
Longtime User
Thanks Erel.

I convert my code to Use JdbcSQL to connect to MySql.

I use your example for this. I have correct connection and read a table with multiple columns.
One of this column (col5) is Blob field in MySql and have an image. This image is saved from Windows Project in MySql as Byte Array

Now i use this code to wait a result from reading with JdbcSQL from MySql database and work fine

B4X:
Wait For (CallSub2(Starter, "SelectStatement", "SELECT * FROM MyTableName")) Complete (Result As List)

The Result is filled with 32 records
Now ....
How i can read blob image from column(5) and save as to SQLite table in Blob Field.
Any simple example ....
Thanks.
 
Upvote 0

dws

Member
Licensed User
Longtime User
Thanks Erel.

I convert my code to Use JdbcSQL to connect to MySql.

I use your example for this. I have correct connection and read a table with multiple columns.
One of this column (col5) is Blob field in MySql and have an image. This image is saved from Windows Project in MySql as Byte Array

Now i use this code to wait a result from reading with JdbcSQL from MySql database and work fine

B4X:
Wait For (CallSub2(Starter, "SelectStatement", "SELECT * FROM MyTableName")) Complete (Result As List)

The Result is filled with 32 records
Now ....
How i can read blob image from column(5) and save as to SQLite table in Blob Field.
Any simple example ....
Thanks.
 
Upvote 0

dws

Member
Licensed User
Longtime User
This code work fine

B4X:
    Wait For (CallSub2(Starter, "MyTableName", SqlQueryString)) Complete (Result As List)
    
    ...
    
    Dim InputStream1 As InputStream
    '-----------------------------------------------+
    ' IN THIS LINE I WANT TO ASSIGN A BLOB FROM RESULT LIST
    ' This : ImputStream1 = ??? Result.Get(5) ???
    ' like that : InputStream1 = File.OpenInput(File.DirAssets, "image.jpg")
    '-----------------------------------------------+
    InputStream1 = File.OpenInput(File.DirAssets, "image.jpg")
    '-----------------------------------------------+
    Dim OutputStream1 As OutputStream
    OutputStream1.InitializeToBytesArray(1000)
    File.Copy2(InputStream1, OutputStream1)
    '-----------------------------------------------+
    Dim Buffer() As Byte
    Buffer = OutputStream1.ToBytesArray

I want help with convesion to comment lines
Any help Please ...
 
Upvote 0

dws

Member
Licensed User
Longtime User
Thank you Erel but i need how to
' This : ImputStream1 = ??? Result.Get(5) ???
In Result list column 5 is blob image

Erel this is full code from Sub.
1. With read local image file work
2. How read the col(5) this is my question
Thans

B4X:
    ' 0. Local variables
    Dim ImgList as List
    
    ' 1. Wait to connection on MySql Database to complete
    Wait For (CallSub(Starter, "Connect")) Complete (Success As Boolean)
    
    ' 2. If Success Connection
    If Success Then
    
        ' 2.1. Query to read all data from my datatable
        Dim s_sqltext As String = "SELECT * FROM " & MyImages

        ' 2.2. Wait to read all data from datatable
        Wait For (CallSub2(Starter, "SelectQueryString", s_sqltext)) Complete (Result As List)
        
        ' 2.3. Check and initialize temporary datalist to assign result
        If ImgList.IsInitialized = False Then
            ImgList.Initialize
        Else
            ImgList.Clear
        End If
        
        ' 2.4. Assign reading result to temporary list of data
        ImgList = Result
        
        '-------------------------------------------------------+
        ' 2.5. Array to reading columns of result
        Dim cols() As String
        
        ' 2.6. If list has records
        If ImgList.Size > 0 Then
        
            ' 2.6.1. For all data get values
            '---------------------------------------------------+
            For i = 0 To ImgList.Size - 1
                '-----------------------------------------------+
                ' Read columns from record
                cols = ImgList.Get(i)
                '-----------------------------------------------+
                ' Insert Query to SQLite
                sSql = "INSERT INTO " & SqliteImageTable & " VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?)"
                '-----------------------------------------------+
                ' Check if SQL initialize
                If SQL.IsInitialized = False Then
                    ' Initialize to SQL database
                    ' Where PathOfDatabase is local path
                    ' Where MyDataBaseName is SQLite database name
                    SQL.Initialize(PathOfDatabase, MyDataBaseName, True)
                End If
                '-----------------------------------------------+
                ' Create a inputstream to read Col(5) because is BLOB in MySql
                ' and i want to convert before save to SQLite
                '-----------------------------------------------+
                Dim InputStream1 As InputStream
                '-----------------------------------------------+ αυτή πρέπει να αντικατασταθεί με αυτή της ανάγνωσης από την βάση MySql
                
                ' HERE I WANT HELP HOW TO READ COL(5) IN InputStream1
                
                ' This example with local image file work fine
                'InputStream1 = File.OpenInput(File.DirAssets, "cat01.jpg")
                
                ' I need to here help
                
                InputStream1 = Col(5) ' ??????? how to get col(5) image bytes '
                
                ' Save a bytes to stream
                '-----------------------------------------------+
                Dim OutputStream1 As OutputStream
                OutputStream1.InitializeToBytesArray(1000)
                File.Copy2(InputStream1, OutputStream1)
                ' Read bytes from output stream
                '-----------------------------------------------+
                Dim Buffer() As Byte
                Buffer = OutputStream1.ToBytesArray
                '-----------------------------------------------+
                ' Fill from columns ( and assign Buffer to Cols(5) )
                '-----------------------------------------------+
                Dim args As List = Array As Object(cols("0"), _
                                                   cols("1"), _
                                                       cols("2"), _
                                                    cols("3"), _
                                                    cols("4"), _
                                                    Buffer, _
                                                    cols("6"), _
                                                    cols("7"), _
                                                    cols("8")  )
                '-----------------------------------------------+
                ' Save to SQLite
                '-----------------------------------------------+
                m_Const.SQL_File.ExecNonQuery2(sSql, args)
                '-----------------------------------------------+
            Next
            '---------------------------------------------------+
        End If
        '-------------------------------------------------------+
    End If
 
Upvote 0

dws

Member
Licensed User
Longtime User
Also i have test this conversion but i receive
Error loading bitmap
B4X:
    ' Get bytes from col(5)
    Dim b() As Byte = cols(5).GetBytes("UTF8")
    ' Read to input stream
    InputStream1.InitializeFromBytesArray(b, 0, b.Length)
 
Upvote 0

dws

Member
Licensed User
Longtime User
B4X:
Dim b() As Byte = Result.Get(5)

Why do you want an input stream?

Beacause i dont know how else to do.
I have test multiple examples from forum but nothing work with me.
I have a post with full sub than i use
 
Upvote 0

dws

Member
Licensed User
Longtime User
This is code to read saved image from SQLite

B4X:
    ' Here read saved image from mysql to assign to ImageView
       Dim c As Cursor = SQL.ExecQuery("SELECT RecImg FROM " & SqliteImageTable  & " WHERE id = " & iRecID)
  
       '-------------------------------------------------------+
       ' check if read record
    If c.RowCount = 1 Then

        c.Position = 0

        '  Read Blob image from SQLite
        Dim Buffer() As Byte = c.GetBlob("RecImg")

        ' Reading to stream
        Dim InputStream1 As InputStream
        InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)

         ' Create a bitmap
        Dim Bitmap1 As Bitmap
        Bitmap1.Initialize2(InputStream1)
        InputStream1.Close

        ' Assing to View
        ImgView.Bitmap = Bitmap1

    End If


And this is returned error

Error occurred on line: 928 (myactivity)
java.lang.RuntimeException: Error loading bitmap.
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:539)
at v4x.estiasi10.myactivity._init_catlist(myactivity.java:959)
at v4x.estiasi10.myactivity._activity_create(myactivity.java:588)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
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 v4x.estiasi10.myactivity.afterFirstLayout(myactivity.java:104)
at v4x.estiasi10.myactivity.access$000(myactivity.java:17)
at v4x.estiasi10.myactivity$WaitForLayout.run(myactivity.java:82)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
** Activity (myactivity) Resume **
 
Last edited:
Upvote 0

dws

Member
Licensed User
Longtime User
This is completely wrong.


This is correct.

Based on the error the data in the blob is not a valid image.

Thank Erel i know ...
I think you don't understund my problem or question

First i read data from MySql and the result is LIST ( give me an example to read as Cursor )

B4X:
Wait For (CallSub2(Starter, "MyTableName", SqlQueryString)) Complete (Result As List)

Second because Result is List i use cols() variable as string and the way to read is

B4X:
Dim b() As Byte = cols(5).GetBytes("UTF8")

How i can use somethink like this ????
B4X:
Dim b() As Byte =Result.GetBlob("RecImg")

Also i have try to use cols() variable as object and i dont know the way to

B4X:
Dim b() As Byte = cols(5).GetBytes
 
Last edited:
Upvote 0

dws

Member
Licensed User
Longtime User
Also i have try to get Result as Cursor but not work
B4X:
 Wait For (CallSub2(Starter, "MyTableName", SqlQueryString)) Complete (Result As Cursor)
 
Last edited:
Upvote 0

dws

Member
Licensed User
Longtime User
Thanks Erel i Resolve the problem and all work properly with JdbcSQL .....

Just change for any like to know
B4X:
Wait For (CallSub2(Starter, "MyTableName", SqlQueryString)) Complete (Result As JdbcResultSet)
This return a Cursor and you can read direcly BLOBs

Thanks...
 
Upvote 0
Top