0
我正在嘗試將圖像保存爲blob類型。 我不確定是否正確。當我調試時,fetchAllItems()和fetchItem()不起作用。 有沒有錯誤?我找不到它.. 對不起,長編碼,因爲我找不到blob的問題。 請幫幫我。謝謝..Sqlite將圖像保存爲blob
錯誤消息在這裏。 12-02 20:38:26.291:I /數據庫(22251): 源碼返回:錯誤代碼= 1,味精=沒有這樣的柱:圖像
public class FridgeDatabaseHelper extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "fridge_db";
private static final int DATABASE_VERSION = 1;
//Database creates through sql statement
private static final String DATBASE_CREATE = "create table fridge_table " +
"(_id integer primary key autoincrement, " +
"category text not null, name text not null, "+"" +
"expired_date text not null, image BLOB);";
public FridgeDatabaseHelper(Context ctxt)
{
//we can put database file name on 'DATABASE_NAME'
super(ctxt, DATABASE_NAME, null, DATABASE_VERSION);
}
//onCreate method
@Override
public void onCreate(SQLiteDatabase db)
{
//Method is called during creation of the database
db.execSQL(DATBASE_CREATE);
}
//Database fields
public static final String KEY_ROWID = "_id";
public static final String KEY_CATEGORY = "category";
public static final String KEY_NAME = "name";
public static final String KEY_EXPIRED_DATE = "expired_date";
public static final String KEY_IMAGE = "image";
private static final String DATABASE_TABLE = "fridge_table";
private Context ctxt;
private SQLiteDatabase db;
private FridgeDatabaseHelper dbhelper;
//SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-YYYY HH:mm:ss");
public FridgeDbAdapter(Context ctxt)
{
this.ctxt = ctxt;
}
//Open database
public FridgeDbAdapter open() throws SQLException
{
dbhelper = new FridgeDatabaseHelper(ctxt);
db = dbhelper.getWritableDatabase();
return this;
}
//Close database
public void close(){
dbhelper.close();
}
//Create a new item
public long insertItem(String category, String name, String expired_date, byte[] image)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_CATEGORY, category);
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_EXPIRED_DATE, expired_date);
initialValues.put(KEY_IMAGE, image);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//return cursor over the list of all items in the database
public Cursor fetchAllItems(){
return db.query(DATABASE_TABLE, new String[]
{KEY_ROWID, KEY_CATEGORY, KEY_NAME, KEY_EXPIRED_DATE, KEY_IMAGE},
null, null, null, null, null);
}
//return a cursor positioned at the defined item
public Cursor fetchItem(long rowId) throws SQLException
{
Cursor mCursor = db.query(true, DATABASE_TABLE,
new String[]{KEY_ROWID, KEY_CATEGORY, KEY_NAME, KEY_EXPIRED_DATE, KEY_IMAGE},
KEY_ROWID + "=" + rowId, null, null, null, null, null);
//byte[] image = null;
if(mCursor != null){
mCursor.moveToFirst();
//image = mCursor.getBlob(mCursor.getColumnIndexOrThrow(KEY_IMAGE));
}
return mCursor;
}
哦..我通過更新數據庫來解決它。謝謝。但是,我再次被卡住保存圖像。當我從相機中選取圖片並保存時,列表視圖不僅一次製作了兩個項目。第一個還是不顯示圖片,但是第二個最後出現選擇的圖片,但是我根本無法修改圖像。 – wholee1
更新數據庫時有什麼變化?當你說你不能修改圖像時,你的意思是你如何更新數據庫中的記錄? – Chris
有時圖像被保存,但有時不會。我真的很想知道。我可以通過電子郵件給我的代碼嗎?你能幫我檢查一下嗎?我真的很抱歉。直到現在我都感到壓力很大。如果可以,你可以給你的電子郵件嗎?謝謝.. – wholee1