java从sqlite检索图像时出错?
我试图将图像存储到SQLite中,然后将图像检索回来。我已经搜索了其他与之相关的帖子,得到了一些线索和线索,但现在我被卡住了,并且出现了错误,我不知道为什么
图像成功地存储到SQLite数据库中,但是当我试图获取图像时,我得到了错误
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
下面是我的代码
SQL帮助程序类
import 安卓.content.Context;
import 安卓.database.sqlite.SQLiteDatabase;
import 安卓.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 安卓.provider.BaseColumns;
import 安卓.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", "");
我得到了所有其他数据,如姓名、电子邮件、所有者id,但当我提到图像时,我得到了错误
这是导致错误的那条线
byte [] image = cursor.getBlob(statusImage);
任何帮助都将不胜感激
# 1 楼答案
你的
projection
没有COLUMN_IMAGE
,所以cursor.getColumnIndex(SQL_Constants.SavedIntoSQL.COLUMN_IMAGE)
返回-1。试图读取列-1会导致此异常如果图像斑点足够小,将列添加到投影中会有所帮助。Android SQLite游标有一个大小有限的窗口,实际上不能查询大于2MB的行。如果是这样的话,请考虑重构代码,这样就不会将图像存储在数据库中。而是将它们存储在文件系统中,并将它们的路径存储在数据库中