如何获取媒体存储中照片的拍摄日期值?

huangapple go评论70阅读模式
英文:

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

问题

该代码分析照片的情感,方法是获取手机的MediaStore中的所有照片,并将其中包含脸部的照片发送到我们的模型。然而,在每次启动时,我们不需要再次查看整个MediaStore,只需查看新增加的照片。

dateTaken 始终返回 "0",而 imagePath 返回有值。

imagePath 的值为真,但 dateTaken 的值为假。

这段代码用于获取照片列表:

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

此代码块用于获取每张照片的 dateTaken,但似乎它对于每张照片都返回零。

英文:

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(&quot;last_photo_date&quot;, -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 + &quot; DESC&quot;;
if (lastPhotoDate != -1){
selection = MediaStore.Images.Media.DATE_TAKEN + &quot;&gt;?&quot;;
selectionArgs = new String[] { String.valueOf(lastPhotoDate) };
}
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
sortOrder
);
if (cursor != null &amp;&amp; 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(&quot;last_photo_date&quot;, 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&lt;Face&gt; sparseArray = faceDetector.detect(frame);
for (int i = 0; i &lt; 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));   

答案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 + " 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();
    }
}

解决了访问MediaStore.Images.Media.DATE_TAKEN属性的问题,改为使用MediaStore.Images.Media.DATE_ADDED属性,现在该代码根据我们的目的正常工作。

英文:
public static void listOfImages(Context context) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
long lastPhotoDate = sharedPreferences.getLong(&quot;last_photo_date&quot;, -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 + &quot; DESC&quot;;
if (lastPhotoDate != -1){
selection = MediaStore.Images.Media.DATE_ADDED + &quot;&gt;?&quot;;
selectionArgs = new String[] { String.valueOf(lastPhotoDate) };
}
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
sortOrder
);
if (cursor != null &amp;&amp; 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(&quot;last_photo_date&quot;, 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&lt;Face&gt; sparseArray = faceDetector.detect(frame);
for (int i = 0; i &lt; 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.

huangapple
  • 本文由 发表于 2023年3月31日 22:39:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定