1、调用SQLiteDatabase的 beginTransaction() 方法来开启一个事务,然后在一个异常捕获的代码块中去执行具体的数据库操作,
2、当所有的操作都完成之后,调用 setTransactionSuccessful() 表示事务已经执行成功了
3、最后在finally代码块中调用 endTransaction() 来结束事务。
/**
* Created by mikeyna on 18-7-3.
*/
public class FaceDetection {
private static String TAG = "FaceDetection";
private ArrayList<String> mPicturePathList = new ArrayList<>();
private Context mContext;
GalleryDatabaseHelper mDBHelper;
public FaceDetection(Context context)
{
mContext = context;
mDBHelper = new GalleryDatabaseHelper(context);
}
public void init(String folderDir)
{
String labelfile = "";
String conf = "/sdcard/tfmodel/caffemodel/facedetector/deploy.prototxt";
String model = "/sdcard/tfmodel/caffemodel/facedetector/res10_300x300_ssd_iter_140000_fp16.caffemodel";
SmartNative.faceDetectorInit(model, conf, labelfile);
getPicturePaths(folderDir);
StorePictureInfo2DB();
}
public void close()
{
SmartNative.faceDetectorClose();
}
//获取相片的名称
private ArrayList<String> getPicturePaths(String dir)
{
mPicturePathList.clear();
mPicturePathList = Files.searchFiles(dir, ".jpg", false, mPicturePathList);
return mPicturePathList;
}
//store picture information to database
private void StorePictureInfo2DB()
{
SQLiteDatabase db = mDBHelper.getReadableDatabase();
for(String imagePath : mPicturePathList)
{
Cursor cursor = db.query(GalleryDatabaseHelper.Tables.PICTURES,
null,
GalleryConstrant.Picture.PATH + " =?", new String[]{imagePath},
null, null, null);
if(cursor != null) {
if(cursor.moveToNext())
continue;
}
ContentValues values = new ContentValues();
values.put(GalleryConstrant.Picture.PATH, imagePath);
db.insert(GalleryDatabaseHelper.Tables.PICTURES, null, values);
}
}
//face detection and store result to database
public void faceDetectAndStoreResult(){
SQLiteDatabase db = mDBHelper.getReadableDatabase();
Cursor cursor = db.query(GalleryDatabaseHelper.Tables.PICTURES,
new String[]{GalleryConstrant.Picture.ID, GalleryConstrant.Picture.PATH},
null, null, null, null,
null, null);
if (cursor == null) {
Log.i(TAG, "cursor is null");
return ;
}
try {
while(cursor.moveToNext()) {
int imageId = cursor.getInt(0);
String imagePath = cursor.getString(1);
ArrayList<LabelInfo> labelInfoList = DetectFace(imagePath);
if(labelInfoList != null)
{
ContentValues values = new ContentValues();
//删除原始记录
db.delete(GalleryDatabaseHelper.Tables.Faces,
GalleryConstrant.Face.PICTURE_ID + "=?",
new String[]{String.valueOf(imageId)});
//将现有的人脸检测结果插入数据库
for(LabelInfo labelInfo : labelInfoList)
{
Gson g = new Gson();
String jsonString = g.toJson(labelInfo.boundingBox);
//add label information to database
values.put(GalleryConstrant.Face.PICTURE_ID, imageId);
values.put(GalleryConstrant.Face.CLUSTER_ID, -1);
values.put(GalleryConstrant.Face.RECT, jsonString);
values.put(GalleryConstrant.Face.FEATURE, "");
db.insert(GalleryDatabaseHelper.Tables.Faces, null, values);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
cursor.close();
}
}
private ArrayList<LabelInfo> DetectFace(String imagePath)
{
if(imagePath.compareTo("") == 0) return null;
ArrayList<LabelInfo> labelInfos = SmartNative.faceDetect(imagePath);
return labelInfos;
}
}
4、数据库是什么时候用?
- 比如金额交易,数据的批量删除和存储等。比如现在要进行银行转账,不可能扣了A的钱,但是B没有收到,然后A的钱就不知道哪里去了,再比如,现在要备份短信,要么成功备份到磁盘,要么备份失败,而不可能在没有备份成功的情况就把本地给删除了。