【问题标题】:How to get the date when an image was taken from MediaStore if I have the file path?如果我有文件路径,如何获取从 MediaStore 拍摄图像的日期?
【发布时间】:2023-03-30 09:50:01
【问题描述】:

我知道我可以使用以下代码检索图像的 id:

    String[] projection = { MediaStore.Images.Media._ID };

    String selection = MediaStore.Images.Media.DATA + " = ?";
    String[] selectionArgs = new String[] { mediaFile.mediaFile().getAbsolutePath() };

    Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    ContentResolver contentResolver = context.getContentResolver();
    Cursor cursor = contentResolver.query(queryUri, projection, selection, selectionArgs, null);
    if(cursor!=null) {
        if (cursor.moveToFirst()) {
            long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
        }
        cursor.close();
    }

我想知道,是否有任何可能的方法来 MediaStore.Images.Media.DATA_TAKENMediaStore.Images.Media.LONGITUDEMediaStore.Images.Media.LATITUDE 使用相同的方法?

【问题讨论】:

    标签: android cursor android-contentresolver


    【解决方案1】:

    如果你有文件路径,你可以通过这种方式获取日期:

    File file = new File(filePath);
    if(file.exists()) //Extra check, Just to validate the given path
    {
        Date lastModDate = new Date(file.lastModified());    
        Log.i("Dated : "+ lastModDate.toString());//Dispaly lastModDate. You can do/use it your own way
    }
    

    如果可用,则可以从图像的EXIF 数据中找到它:

    ExifInterface intf = null;
    try 
    {
        intf = new ExifInterface(path);
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    
    if(intf != null)
    {
        String dateString = intf.getAttribute(ExifInterface.TAG_DATETIME);
        Log.i("Dated : "+ dateString.toString()); //Dispaly dateString. You can do/use it your own way
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      相关资源
      最近更新 更多