从SQLite检索图像给出错误?

时间:2019-05-01 19:09:30

标签: java android

我正在尝试将图像存储到SQLite中,然后取回图像。我搜索了与此相关的其他帖子,并获得了一些线索和线索,但现在我陷入了困境,并且收到了我不知道为什么的错误消息?

图像成功存储到SQLite数据库中,但是当我尝试获取图像时出现错误

  

原因:java.lang.IllegalStateException:无法从CursorWindow读取行0,col -1。在从游标访问数据之前,请确保已正确初始化游标。

下面是我的代码

SQL Helper类

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLHelperClass  extends SQLiteOpenHelper {



    public static  int DATABASE_VERSION = 12;
    public static  String DATABASE_NAME = "user_image_save.db";

    public static  String CREATE_TABLE = "CREATE TABLE";
    public static  String TEXT_TYPE = "TEXT";
    public static String BLOB = "BLOB";
    public static  String INTEGER_TYPE = "INTEGER";
    public static  String DROP_TABLE = "DROP TABLE IF EXISTS" ;
    public static  String AUTOINCREMENT = "INTEGER PRIMARY KEY AUTOINCREMENT" ;


    public SQLHelperClass(Context context ){
        super(context , DATABASE_NAME , null , DATABASE_VERSION);

    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_TABLE + " " + SQL_Constants.SavedIntoSQL.TABLE_SQL_SAVE_IMAGE

                + " ( " + SQL_Constants.SavedIntoSQL._ID + " " + AUTOINCREMENT + " , "
                +         SQL_Constants.SavedIntoSQL.COLUMN_OWNER_ID + " " + TEXT_TYPE + " , "
                +         SQL_Constants.SavedIntoSQL.COLUMN_NAME + " " + TEXT_TYPE + " , "
                +         SQL_Constants.SavedIntoSQL.COLUMN_EMAIL + " " + TEXT_TYPE + " , "
                +         SQL_Constants.SavedIntoSQL.COLUMN_IMAGE + " " + BLOB+ ")" );

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {

        sqLiteDatabase.execSQL(DROP_TABLE);
        onCreate(sqLiteDatabase);
    }
}

常数类

import android.provider.BaseColumns;


import android.provider.BaseColumns;

public final class SQL_Constants {

    public static abstract class SavedIntoSQL implements BaseColumns {

        public static final String TABLE_SQL_SAVE_IMAGE = "imageSave";

        public static final String ID_DATA = _ID ;
        public static final String COLUMN_OWNER_ID = "owner_id";
        public static final String COLUMN_EMAIL = "email";
        public static final String COLUMN_NAME = "name";
        public static final String COLUMN_IMAGE = "user_image";



    }

}

将数据存储到SQLite

 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                                                    resource.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
                                                    byte[] bytes = byteArrayOutputStream.toByteArray();


                                                    Toast.makeText(UpdatedUserProfile.this, "Download Complete", Toast.LENGTH_SHORT).show();

                                                    ContentValues contentValues = new ContentValues();

                                                    contentValues.put(SQL_Constants.SavedIntoSQL.COLUMN_OWNER_ID, App.getAppInstance().getCurrentUser().getProperty("ownerId").toString());
                                                    contentValues.put(SQL_Constants.SavedIntoSQL.COLUMN_NAME, App.getAppInstance().getCurrentUser().getProperty("name").toString());
                                                    contentValues.put(SQL_Constants.SavedIntoSQL.COLUMN_EMAIL, App.getAppInstance().getCurrentUser().getEmail());
                                                    contentValues.put(SQL_Constants.SavedIntoSQL.COLUMN_IMAGE, bytes);


                                                    sqLiteDatabase.insert(SQL_Constants.SavedIntoSQL.TABLE_SQL_SAVE_IMAGE, null, contentValues);

取回数据

sqlHelperClass = new SQLHelperClass(UpdatedUserProfile.this);
        sqLiteDatabase = sqlHelperClass.getWritableDatabase();
        sqLiteDatabase = sqlHelperClass.getReadableDatabase();

        String projection [] = {

                SQL_Constants.SavedIntoSQL.COLUMN_OWNER_ID ,
                SQL_Constants.SavedIntoSQL.COLUMN_EMAIL ,
                SQL_Constants.SavedIntoSQL.COLUMN_NAME ,


        };


        cursor =  sqLiteDatabase.query(SQL_Constants.SavedIntoSQL.TABLE_SQL_SAVE_IMAGE , projection,
                null , null , null , null , null);



            cursor.moveToPosition(0);

            int statusOwnerId = cursor.getColumnIndex(SQL_Constants.SavedIntoSQL.COLUMN_OWNER_ID);
            int statusName = cursor.getColumnIndex(SQL_Constants.SavedIntoSQL.COLUMN_NAME);
            int statusEmail = cursor.getColumnIndex(SQL_Constants.SavedIntoSQL.COLUMN_EMAIL);
            int statusImage = cursor.getColumnIndex(SQL_Constants.SavedIntoSQL.COLUMN_IMAGE);

            String ownerIdAgain = cursor.getString(statusOwnerId);
            String name = cursor.getString(statusName);
            String email = cursor.getString(statusEmail);
            byte [] image = cursor.getBlob(statusImage);

            String compareValueOwnerId = sharedPreferencesDatabaseActivity.getString("userId", "");
            String compareValueUserName = sharedPreferencesDatabaseActivity.getString("userName", "");
            String compareValueEmail = sharedPreferencesDatabaseActivity.getString("userEmail", "");

我正在获取所有其他数据,例如name,email,owner_id,但是当我拍摄图像时出现错误

这是导致错误的行

  

byte []图片= cursor.getBlob(statusImage);

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您的projection没有COLUMN_IMAGE,因此cursor.getColumnIndex(SQL_Constants.SavedIntoSQL.COLUMN_IMAGE)返回-1。尝试读取列-1会导致此异常。

如果图像斑点足够小,则将列添加到投影中会有所帮助。 Android SQLite游标的窗口大小有限,实际上,您不能查询大于2MB的行。如果是这种情况,请考虑重构代码,以免将图像存储在数据库中。而是将它们存储在文件系统中,而仅将其路径存储在数据库中。