简体   繁体   English

将图像添加到android SQLite数据库

[英]Adding image to android SQLite Database

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. 我有带图片和文本的微调框,它已连接到SQLite数据库。 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: 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;
        }
    }

}

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)

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

It's likely that your mDb database variable has not been initialized correctly, viz your database provider is null. 您的mDb数据库变量可能未正确初始化,即您的数据库提供程序为null。 I normally separate the Helper and DataProvider classes rather than merge them into one class as you have done. 我通常将HelperDataProvider类分开,而不是像您那样将它们合并为一个类。

I would recommend you follow the same methodology as show in Lars Vogel's excellent SQLite tutorial . 我建议您采用与Lars Vogel出色的SQLite教程中显示的方法相同的方法。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM