将图像添加到android SQLite数据库

时间:2013-09-14 04:07:58

标签: android sqlite spinner

我尽力解决这个问题,但无法弄明白。

我有带有图像和文本的微调器,它连接到SQLite数据库。基本上,当我单击“保存”按钮时,我希望选择的微调器图像保存在数据库中。但它并没有像预期的那样发生。

以下是代码。任何帮助表示赞赏。

MainActivity代码:

public void saveTask(){
    if(i == 0){
        Bitmap im = BitmapFactory.decodeResource(getResources(), R.drawable.red);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        im.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] imageinByte = stream.toByteArray();
        long id = mDbHelper.addImage(imageinByte);
        if(id > 0){
            mRowId = id;
        }
    }

}

数据库代码:

private static final String DATABASE_NAME = "Spinnner";
private static final String DATABASE_TABLE = "spin";
private static final int DATABASE_VERSION = 1;

public static final String KEY_ROWID = "_id";
public static final String KEY_IMAGE = "image";

private static final String TAG = "SPINNERERROR";

private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

private static final String DATABASE_CREATE = 
        "CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE + " ("
        + KEY_ROWID + " integer primary key autoincrement, "
        + KEY_IMAGE + " BLOB);";

private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper{
    DatabaseHelper(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    public void onCreate(SQLiteDatabase db){
        db.execSQL(DATABASE_CREATE);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }
}
public Database(Context ctx){
    this.mCtx = ctx;
}
public Database open() throws SQLException{
    mDbHelper = new DatabaseHelper(mCtx);
    mDb = mDbHelper.getWritableDatabase();
    return this;
}
public void close(){
    mDbHelper.close();
}
public long addImage(byte[] image){
    ContentValues cv = new ContentValues();
    cv.put(KEY_IMAGE, image);
    return mDb.insert(DATABASE_TABLE, null, cv);
}

Log cat:

09-14 00:28:29.228: E/AndroidRuntime(30838): FATAL EXCEPTION: main
09-14 00:28:29.228: E/AndroidRuntime(30838): java.lang.NullPointerException
09-14 00:28:29.228: E/AndroidRuntime(30838):    at        com.example.spinnerimage.Database.addImage(Database.java:62)
09-14 00:28:29.228: E/AndroidRuntime(30838):    at   com.example.spinnerimage.MainActivity.saveTask(MainActivity.java:114)
09-14 00:28:29.228: E/AndroidRuntime(30838):    at com.example.spinnerimage.MainActivity.onClick(MainActivity.java:102)

2 个答案:

答案 0 :(得分:0)

从此改变:

private ContentValues createContentValues(byte[] Image) {
    ContentValues values = new ContentValues();
    values.put(KEY_IMAGE, Image);

    return values;
}

public long addImage(byte[] image){
ContentValues cv = createContentValues(image);
return mDb.insert(DATABASE_TABLE, null, cv);
}

答案 1 :(得分:0)

您的mDb数据库变量可能尚未正确初始化,即您的数据库提供程序为null。我通常将HelperDataProvider类分开,而不是像你一样将它们合并到一个类中。

我建议您按照Lars Vogel's excellent SQLite tutorial中显示的方法进行操作。

希望这有帮助。