dimanche 23 août 2015

Issue with Android SQLite cursor while reading table having blob content

I'm getting following exception reported by few users of my application.However I'm not getting the same when trying on my local device. Would need advice as to how to approach towards solving the problem.

java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. at android.database.CursorWindow.nativeGetLong(Native Method) at android.database.CursorWindow.getLong(CursorWindow.java:507) at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:75) at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:220) at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:237)

Following are the details for the context:

The exception is coming on the table storing the images as blob. The table definition is photo_id String, blob_image Blob blob_date String

The code to get the blob data is as following

byte[] photo= null;
Cursor cursor = null;
String criteria = " photo_id = ?";
String[] arguments = {photoid}; 
try {
    cursor = database.query("MY_GALLERY", null,criteria, arguments, null, null, null);
    if(null !=cursor){
        int rowCount = cursor.getCount();
        if(rowCount>0){
            cursor.moveToFirst();
            int colIndex = cursor.getColumnIndex("blob_image");
            if (colIndex != -1) {
                photo = cursor.getBlob(colIndex));
            }           
        }
   }
} catch (Exception e) {
    e.printStackTrace();
}

As I'm trying to fetch blob value only if the row count is greater than 0 after checking cursor.getCount() I find it difficult why cusrsor initialization is being raised as issue. I also tried to check the size of image being saved. They are less than 400kb in size.

This error is also reported from users using Nexus 7 with Android version 4.4.4 and Samsung tablet SM-T116NY with android version 4.4.

The application is in use for over two years and I'm getting this issue for the first time .

I've checked similar and related issues already and most of the suggestions mentioned [with respect appropriate data base initialisation, handling of cursor index, opening/closing of cursor, row size limit less than 2mb are taken care of]

One of the reason to store this in DB is to have an alternate storage where user do not delete stuff by mistake. In application the image from DB is used only if the image is not found in file system.

Any recommendation or suggestion?

thanks Pradeep

Aucun commentaire:

Enregistrer un commentaire