javaandroid-studioandroid-galleryandroid-cursor

how can i get date_taken value of photo in media store?


The app analyzes emotion by taking all the photos in the phone's MediaStore and sending the photos containing the face(s) in the loop to our model. However, instead of looking at the whole MediaStore again at each launch, we only need to look at the newly added photos.

dateTaken always returns "0" while imagePath returns value.

imagePath's value is true but dateTaken's value 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();
    }
}

this code block returns zero for each photo:

long dateTaken = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_TAKEN));   
        

Solution

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

    We were experiencing an issue because we couldn't access the MediaStore.Images.Media.DATE_TAKEN property of MesiaStore. I resolved the issue by using the DATE_ADDED property of MesiaStore instead. Now this code is working properly according to our purpose.