该应用通过拍摄手机mediaststore中的所有照片并将包含循环中的面部的照片发送给我们的模型来分析情绪。但是,我们只需要查看新添加的照片,而不是每次发布时再次查看整个mediaststore。
dateTaken总是返回"0"而imagePath返回值
imagePath的值为true,而dateTaken的值为false。
public static void listOfImages(Context context) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
long lastPhotoDate = sharedPreferences.getLong("last_photo_date", -1);
String[] projection = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_TAKEN };
String selection = null;
String[] selectionArgs = null;
String sortOrder = MediaStore.Images.Media.DATE_TAKEN + " DESC";
if (lastPhotoDate != -1){
selection = MediaStore.Images.Media.DATE_TAKEN + ">?";
selectionArgs = new String[] { String.valueOf(lastPhotoDate) };
}
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
sortOrder
);
if (cursor != null && cursor.moveToFirst()) {
do {
String imagePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
long dateTaken = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_TAKEN));
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong("last_photo_date", dateTaken);
editor.apply();
final Bitmap myBitmap = BitmapFactory.decodeFile(imagePath);
FaceDetector faceDetector = new FaceDetector.Builder(context.getApplicationContext())
.setTrackingEnabled(false)
.setLandmarkType(FaceDetector.ALL_LANDMARKS)
.setMode(FaceDetector.FAST_MODE)
.build();
Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
SparseArray<Face> sparseArray = faceDetector.detect(frame);
for (int i = 0; i < sparseArray.size(); i++) {
Face face = sparseArray.valueAt(i);
Bitmap faceBitmap = Bitmap.createBitmap(
myBitmap,
(int) face.getPosition().x,
(int) Math.abs(face.getPosition().y),
(int) face.getWidth(),
(int) face.getHeight());
classifyEmotions(faceBitmap, context, imagePath);
}
} while (cursor.moveToNext());
cursor.close();
}
}
这个代码块对每张照片返回0:
long dateTaken = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_TAKEN));
public static void listOfImages(Context context) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
long lastPhotoDate = sharedPreferences.getLong("last_photo_date", -1);
String[] projection = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_ADDED };
String selection = null;
String[] selectionArgs = null;
String sortOrder = MediaStore.Images.Media.DATE_ADDED + " DESC";
if (lastPhotoDate != -1){
selection = MediaStore.Images.Media.DATE_ADDED + ">?";
selectionArgs = new String[] { String.valueOf(lastPhotoDate) };
}
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
sortOrder
);
if (cursor != null && cursor.moveToFirst()) {
do {
String imagePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
long dateTaken = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_ADDED));
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong("last_photo_date", dateTaken);
editor.apply();
final Bitmap myBitmap = BitmapFactory.decodeFile(imagePath);
FaceDetector faceDetector = new FaceDetector.Builder(context.getApplicationContext())
.setTrackingEnabled(false)
.setLandmarkType(FaceDetector.ALL_LANDMARKS)
.setMode(FaceDetector.FAST_MODE)
.build();
Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
SparseArray<Face> sparseArray = faceDetector.detect(frame);
for (int i = 0; i < sparseArray.size(); i++) {
Face face = sparseArray.valueAt(i);
Bitmap faceBitmap = Bitmap.createBitmap(
myBitmap,
(int) face.getPosition().x,
(int) Math.abs(face.getPosition().y),
(int) face.getWidth(),
(int) face.getHeight());
classifyEmotions(faceBitmap, context, imagePath);
}
} while (cursor.moveToNext());
cursor.close();
}
}
我们遇到了一个问题,因为我们无法访问mediaststore . images . media。mesistore的DATE_TAKEN属性。我通过使用MesiaStore的DATE_ADDED属性来解决这个问题。现在这段代码按照我们的目的正常工作了。