我正在尝试在SQLite数据库中保存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
答案 0 :(得分:1)
小心,您的数据库基本上位于内部存储器中,而将图像保存在外部存储器中很容易。这样做会增加应用程序的大小...为什么不在db中保存文件的路径,并将其保存在内部存储中?