我正在嘗試從SQLite數據庫中的Android手機保存圖像。下面是代碼:在Android中將數據保存在數據庫中
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera c) {
currentPictureName = Utils.getUserName(getApplicationContext())
+ System.currentTimeMillis() + ".jpg";
DBHelper helper = new DBHelper(getApplicationContext());
SQLiteDatabase db = helper.getReadableDatabase();
ContentValues values = new ContentValues();
values.put("name", currentPictureName);
values.put("image", data);
//db.insert(DBHelper.TABLE_NAME, null, values);
DBHelper.insertDB(db, DBHelper.TABLE_NAME, null, values);
mCamera.startPreview();
}
};
DBHelper.java
package com.apps.terrapin;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBHelper extends SQLiteOpenHelper {
private static Context mContext;
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "terrapin";
public static final String TABLE_NAME = "images";
private static final String TABLE_CREATE =
"CREATE TABLE " + TABLE_NAME + " (" +
"_id INTEGER PRIMARY KEY, name TEXT, image BLOB);";
DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
}
public static void insertDB(final SQLiteDatabase db, String tableName, String hack, final ContentValues values) {
Log.e("TAG", "Insert DB");
new Thread(new Runnable() {
public void run() {
db.insert(DBHelper.TABLE_NAME, null, values);
}
}).start();
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.e("TAG", "Creating DB");
db.execSQL(TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion >= newVersion)
return;
onCreate(db);
}
}
的問題是,我的應用程序正確地將圖像保存了幾次,然後當我點擊保存圖像按鈕,它掛起。然後,Android要求我強制關閉它。我有正常的insert
方法這個問題,所以我認爲插入一個線程可能會有所幫助。但事實並非如此。這裏有什麼問題? logcat中不顯示任何錯誤,有一個線,說
12-13 05:02:08.005: I/InputDispatcher(93): Application is not responding:
Window{406d17f8 com.apps.terrapin/com.apps.terrapin.TerraPin paused=false}.
10013.7ms since event, 10012.2ms since wait started
我使用API級10
您需要將圖像存儲爲android中的BLOB, – Abhi
是的,我將圖像列定義爲blob。函數調用不正確嗎? –
你錯過了'insertDB()'中新線程的'start()'嗎? – Karthik