如果我有文件路径,如何获取从MediaStore中获取图像的日期?

时间:2017-01-17 22:44:52

标签: android cursor android-contentresolver

我知道我可以使用以下代码检索图像的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有任何可能的方式使用相同的方法吗?

1 个答案:

答案 0 :(得分:0)

如果你有文件路径,你可以这样得到日期:

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
}
相关问题