我想插入图像,删除图像并从SQLite数据库中检索所有图像。我正在创建数据库,代码如下所示。
但是如何在MainActivity
中调用这些方法?
public class Imagehelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="abhi.db";
private static final int SCHEMA_VERSION=1;
public Imagehelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE Image(_id INTEGER PRIMARY KEY AUTOINCREMENT,imageblob BLOB);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor getAll() {
return(getReadableDatabase().rawQuery("SELECT imageblob FROM Image",null));
}
public void insert(byte[] bytes)
{
ContentValues cv=new ContentValues();
cv.put("imageblob",bytes);
Log.e("inserted", "inserted");
getWritableDatabase().insert("Image","imageblob",cv);
System.out.println(":vlfkv: " +cv.size());
}
public byte[] getImage(Cursor c)
{
return(c.getBlob(1));
}
public void deletePhoto(String id) {
SQLiteDatabase database = this.getWritableDatabase();
String deleteQuery = "DELETE FROM Image where _id='"+ id +"'";
Log.d("query",deleteQuery);
database.execSQL(deleteQuery);
}
}
答案 0 :(得分:0)
您的活动中需要有Imagehelper
的实例:
Imagehelper mImageHelper = new Imagehelper();
// and then you can access your methods ...
mImageHelper.insert(yourValue);
mImageHelper.get(yourValue);
mImageHelper.deletePhoto(yourValue);
我建议您按照Android的一些dabatase教程:
http://www.vogella.com/articles/AndroidSQLite/article.html
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
答案 1 :(得分:0)
使用此代码。其中image是字节数组。将所有图像作为byte[]
代码获取已经编写
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
答案 2 :(得分:0)
您尚未在 getImage 方法中调用选择查询。所以看下面的功能。
public byte[] getImage(
{
Cursor c = database.rawQuery("SELECT imageblob FROM Image",null);
return(c.getBlob(1));
}