在 Android Studio 中,一旦我们的应用程序首次安装并打开,我们就可以将所有保存下载的照片保存在照片文件夹中。但是,如果在第二次登录应用程序时将新照片添加到手机自己的照片中,则它们不会进入我们的应用程序。
public static void listOfImages(Context context) {
Uri uri;
String ablosutePathOfImage;
emotionDatabase =
EmotionDatabase.getEmotionDatabase(context);
Long currentTime = System.currentTimeMillis();
String timeStamp = currentTime.toString();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String lastPhotoDate = sharedPreferences.getString("last_photo_date",null);;
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String [] projection = {MediaStore.MediaColumns.DATA,MediaStore.Images.Media.DATE_TAKEN};
String orderBy = MediaStore.Images.Media.DATE_TAKEN + " DESC";
if(lastPhotoDate == null){
cursor = context.getContentResolver().query(uri,projection,null,null,orderBy);
}else{
cursor = context.getContentResolver().query(uri, projection,
MediaStore.Images.Media.DATE_TAKEN + ">=? and " +
MediaStore.Images.Media.DATE_TAKEN + "<=?",
new String[] {lastPhotoDate , timeStamp},
orderBy);
}
int column_index_data =
cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
//face detection standard classify yapan kısım
if(cursor.moveToFirst()){
ablosutePathOfImage =
cursor.getString(column_index_data);
File file = new File(ablosutePathOfImage);
Long photoDate = file.lastModified();
lastPhotoDate = photoDate.toString();
SharedPreferences.Editor editor =
sharedPreferences.edit();
editor.putString("last_photo_date", lastPhotoDate);
editor.apply();
do {
ablosutePathOfImage = cursor.getString(column_index_data);
final Bitmap myBitmap = BitmapFactory.decodeFile(ablosutePathOfImage);
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);
float x1 = face.getPosition().x;
float y1 = face.getPosition().y;
Bitmap faceBitmap = Bitmap.createBitmap(
myBitmap,
(int) x1,
(int) y1,
(int) face.getWidth(),
(int) face.getHeight());
classifyEmotions(faceBitmap, context, ablosutePathOfImage);
}
} while (cursor.moveToNext());
}
}
使用 SharedPreferences,我们在应用程序中访问 Long 类型手机照片中最后扫描照片的时间。并保存它。
应用程序第二次运行时,它应该从 lastPhotoDate 值到光标中的时间戳值获取照片。
我检查了 lastPhotoDate 值,它返回应用程序扫描的最后一张照片的值。 timeStampasi 变量返回应用程序启动时间的值。
在第二次运行中,它不会以任何方式输入 if (cursor.moveToFirst()){}。 Cursor 的值或逻辑错误,我无法弄清楚。