Adding image to android SQLite Database

2019-03-03 17:49发布

问题:

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)

回答1:

change from this:

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


回答2:

It's likely that your mDb database variable has not been initialized correctly, viz your database provider is null. I normally separate the Helper and DataProvider classes rather than merge them into one class as you have done.

I would recommend you follow the same methodology as show in Lars Vogel's excellent SQLite tutorial.

Hope this helps.