如何在 Android Studio 中从 MediaStore 获取 2 个不同时间段的照片?

问题描述 投票:0回答:1

应用程序通过拍摄手机 MediaStore 中的所有照片并将循环中包含人脸的照片发送到我们的模型来分析情绪。然而,我们不需要在每次启动时再次查看整个 MediaStore,我们只需要查看新添加的照片。

但是,当我们第一次安装应用程序时,所有照片都成功检索,但是当我们退出应用程序,下载新照片并重新进入应用程序时,我们遇到了只有新添加的照片没有检索到的问题。

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());
    }
}

我们在 lastPhotoDate 变量中以秒为单位保存图库中最后一张照片的最后照片日期,并使用 SharedPreferences 存储此数据。

如果我们存储在SharedPreferences中的last_photo_date为空,则lastPhotoDate变量赋值为“null”,如果不为空,则赋值为last_photo_date。

应用程序运行时,如果从lastPhotoDate变量中的值到保存系统时间的timeStamp变量的值的时间段内有任何新照片,则将它们分配给光标。

我检查了 lastPhotoDate 变量的值,它返回了应用程序扫描的最后一张照片的值。 timeStamp 变量返回 listOfImages 函数运行时手机的当前时间值。

当我添加一张新照片并打开应用程序时,它永远不会进入“if (cursor.moveToFirst()) {}”语句。

我的游标查询可能有错误:

cursor = context.getContentResolver().query(uri, projection,
   MediaStore.Images.Media.DATE_TAKEN + ">? and " + 
   MediaStore.Images.Media.DATE_TAKEN + "<?",
   new String[] {lastPhotoDate , timeStamp},
   orderBy);

我获取最后一张照片日期的代码块可能有误:

ablosutePathOfImage = cursor.getString(column_index_data);
File file = new File(ablosutePathOfImage);
Long photoDate = file.lastModified();
lastPhotoDate = photoDate.toString();
SharedPreferences.Editor editor editor = sharedPreferences.edit();
editor.putString("last_photo_date", lastPhotoDate);
editor.apply();
java android android-gallery android-cursor
1个回答
0
投票
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 + " ASC";

    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();
    }
}

我们遇到了一个问题,因为我们无法访问 MesiaStore 的 MediaStore.Images.Media.DATE_TAKEN 属性。我编辑了代码并通过使用 MesiaStore 的 DATE_ADDED 属性解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.