0

I tried my best to solve this problem, but can't figure it out.

I have spinner with image and text, which is connected to SQLite database. Basically when I click save button, I want selected spinner image to save in database. But it is not happening as expected.

Below is the code. Any help is appreciated.

MainActivity code:

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;
        }
    }

}

DATABASE CODE:

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)
4

2 回答 2

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);
}
于 2013-09-14T05:07:51.660 回答
0

您的mDb数据库变量可能未正确初始化,即您的数据库提供程序为空。我通常将HelperandDataProvider类分开,而不是像您所做的那样将它们合并为一个类。

我建议您遵循与Lars Vogel 出色的 SQLite 教程中所示相同的方法。

希望这可以帮助。

于 2013-09-14T05:17:33.083 回答