Android Question Error when read large BLOB images

Jose Cuevas

Member
Licensed User
Longtime User
Hello, I'm saving the contents of an ImageView into a BLOB field, in another option I read that field and show it in another ImageView.

Everything works fine, if is a small image (low resolution), but if is a high resolution image, it saves well, but when I want to read it gives me this error:

java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

Thanks in advance for any help.
 

Jose Cuevas

Member
Licensed User
Longtime User
B4X:
Sub SaveFoto

    FotoDB.ExecNonQuery("DELETE FROM Fotos")
   
    Dim bFoto2 As Bitmap
    Dim imgBuffer() As Byte

    bFoto2 = ImageView1.Bitmap
   
    Dim out As OutputStream
    out.InitializeToBytesArray(0)
    bFoto2.WriteToStream(out, 0, "PNG")
   
    imgBuffer = out.ToBytesArray

    out.Close
   
    FotoDB.ExecNonQuery2("INSERT INTO Fotos VALUES(?)", Array As Object(imgBuffer))
   
End Sub

B4X:
Sub ReadFoto

    Dim cFoto As Cursor
    Dim bFoto As Bitmap
   
    cFoto = FotoDB.ExecQuery("SELECT Foto FROM Fotos")
    If cFoto.RowCount >= 1 Then
        cFoto.Position=0
           
        Dim Buffer() As Byte
        Buffer = cFoto.GetBlob("Foto")   '*********** In this line is the Error ************
        Dim InputStream1 As InputStream
        InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
           
        bFoto.Initialize2(InputStream1)
        InputStream1.Close
        ImageView1.Bitmap = bFoto
        cFoto.Close
    End If
   
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
SqLite has a cursor-limit of 2mb. So the result of an Query can not exceed 2mb...
 
Upvote 0

eps

Expert
Licensed User
Longtime User
This https://stackoverflow.com/questions/17300407/access-large-blob-in-android-sqlite-without-cursor might help but it might just open up pandoras box... of course there's sqlite and sqlite....

You need to consider device limitations and other issues...

What are you attempting to do with storing blobs in the database?

Is it possible to compress the file size in some way before storing (without too much degradation?)

Maybe consider storing the images on the device, in the filesystem itself and then just storing the filename and location in the database?

Of course either way at some point you will use up all the space or memory available.. You need to consider what happens at that point.
 
Upvote 0

Jose Cuevas

Member
Licensed User
Longtime User
FYI: I did reduce the image before save it into the database and works like a charm, the image doesn't degrade.

B4X:
Sub SaveFoto

    FotoDB.ExecNonQuery("DELETE FROM Fotos")
   
    Dim bFoto2 As Bitmap
    Dim imgBuffer() As Byte
   
    bFoto2 = CreateScaledBitmap(ImageView1.Bitmap, 960,960, True)
   
    Dim out As OutputStream
    out.InitializeToBytesArray(0)
    bFoto2.WriteToStream(out, 0, "PNG")
   
    imgBuffer = out.ToBytesArray

    out.Close
   
    FotoDB.ExecNonQuery2("INSERT INTO Fotos VALUES(?)", Array As Object(imgBuffer))
   
End Sub

Sub CreateScaledBitmap(Original As Bitmap, Width As Int, Height As Int, Filter As Boolean) As Bitmap
   
    Dim r As Reflector
    Dim b As Bitmap
   
    b = r.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
        Array As Object(Original, Width, Height, Filter), _
        Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
       
    Return b
   
End Sub
 
Upvote 0
Top