我想浏览图库中的图片并将其保存在sqlite数据库中
插入图片时出现以下错误
UserModel.findOne({ firstName: "bob" }, function(err, user) {
UserModel.update(user, {
$push: {
pets: {
$each: [{ name: "felix", type: "cat" }],
$slice: -10
}
}
}, function(err, numAffected) {
if (err) console.log(err);
console.log('updated');
});
});
代码:
java.lang.NullPointerException
at com.example.imgapp.DBAdapter.insert(DBAdapter.java:91)
at com.example.imgapp.MainActivity.insert_img(MainActivity.java:76)
public class DBAdapter {
Context context;
DBHelper DBH;
SQLiteDatabase db;
DBAdapter(Context cx)
{
this.context = cx;
DBH = new DBHelper(context);
}
String DB_name="imagesDB";
int DB_version=1 ;
String table_name="images";
String column1="id";
String column2="title";
String column3="image";
String create_table="Create TABLE"+ table_name +"("+column1+" INTEGER PRIMARY KEY," + column2 +"TEXT,"+column3 +"BLOB );";
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, DB_name, null, DB_version);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.v("database creation", "creating ...................");
db.execSQL(create_table);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i2) {
Log.v("upgrade database","upgrading.............");
db.execSQL("DROP Table IF EXISTS images");
onCreate(db);
}
}
public DBAdapter open() throws SQLException
{
DBH.getWritableDatabase();
return this;
}
public void close()
{
DBH.close();
}
public long insert(String title ,byte[] img )
{
ContentValues cv=new ContentValues();
cv.put("title",title);
cv.put("image",img);
return db.insert(table_name,null,cv);
}
}
从图像视图插入图像时出现问题 问题是什么?有什么帮助吗? 提前谢谢....
答案 0 :(得分:1)
您需要保留getWritableDatabase
函数的返回值并使用该函数调用insert
。现在,您正在使用db
变量而不初始化它而忽略了getWritableDatabase
的结果。