Reputation: 2560
I am trying to save images from an Android phone in a SQLite DB. Here is the code:
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);
}
}
The problem is, my app saves images properly a few times and then when I click on the save image button, it hangs. Then Android asks me to force close it. I had this problem with the normal insert
method so I thought putting the insert in a thread might help. But it did not. What is wrong here? Logcat does not show any errors, there is one line which says
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
I am using API level 10
Upvotes: 1
Views: 1464
Reputation: 521
Carefull, your db is basically in your internal memory, while it's easy to save picture in external storage. Doing this will increase the size of your app... Why don't save in your db the path to the file, and save it out the internal storage?
Upvotes: 1