You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MediaStore获取照片DATE_TAKEN字段值始终为0的问题求助

MediaStore获取照片DATE_TAKEN字段值始终为0的问题求助

大家好,我最近在开发一个分析照片人脸情绪的应用,遇到了个棘手的问题,想请教下各位:

我的应用逻辑是遍历手机MediaStore里的所有照片,把包含人脸的照片送到模型分析情绪。为了优化性能,避免每次启动都重新扫全部照片,我打算只处理新增的照片——通过记录上次处理的最后一张照片的拍摄日期,下次启动时只查询DATE_TAKEN大于这个日期的照片。

但现在遇到的问题是:MediaStore.Images.Media.DATE_TAKEN字段总是返回0,而MediaStore.Images.Media.DATA(也就是imagePath)能正常获取到有效路径。简单说就是,imagePath的取值是正常的,但DATE_TAKEN始终拿不到正确的拍摄时间,一直是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_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));
            
            // 这里dateTaken总是返回0
            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();
    }
}

问题就出在这行代码:

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

不管哪张照片,这行返回的dateTaken都是0,导致我的新增照片筛选逻辑完全失效,每次启动还是会扫描全部照片。有没有朋友遇到过类似的问题?或者知道可能是什么原因导致的?麻烦帮忙指点一下,谢谢大家!

备注:内容来源于stack exchange,提问作者Begüm Muşdal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 10:59:11