拍摄时显示照片的日期?

时间:2012-09-18 14:12:06

标签: android date imageview

我希望在我的ImageView上显示照片的日期。让我们说我的照片上有照片。一旦我点击它,它将转到其他活动并显示图像的完整大小。现在,在该活动中,我需要显示拍摄日期。任何想法?

3 个答案:

答案 0 :(得分:7)

我可以通过两种方式在Android上看到这样做。第一个是文件修改日期,可以这样实现:

String pathToFile = "Your file path";
File file = new File(pathToFile);
if(file.exists()) {
    long date = file.lastModified();
    /* Do your modified date stuff here */
}

另一种方法是检查图像上的EXIF数据,以获取该信息可用的日期:

ExifInterface intf = null;
try {
    intf = new ExifInterface(path);
} catch(IOException e) {
    e.printStackTrace();
}

if(intf == null) {
    /* File doesn't exist or isn't an image */
}

String dateString = intf.getAttribute(ExifInterface.TAG_DATETIME);
/* Do your date/time stuff here */

请注意,图片的Exif数据可能不包含日期。大多数手机摄像头和商用摄像头都会添加日期/时间Exif条目,但在计算机上创建的图像不太可能有Exif数据。此外,Exif日期/时间格式不是很好。示例Exif日期/时间字符串:2012:06:02 14:33:46(取自我使用Nexus S拍摄的图像)

答案 1 :(得分:2)

如果您只是将每个图像作为File从外部存储中读取,则File会有一个名为lastModified()的方法来获取文件的修改日期。虽然这不一定是创建文件的日期,但对于像图像这样的不可变文件,它很可能是。

for (File file : files) {
    //This is the date the file was modified/created
    long date = file.lastModified();
    //You can even convert it to a Date if you want
    Date fileData = new Date(date);
}

如果您的应用程序是在外部存储上创建映像的应用程序,并且您绝对只需要文件的初始创建日期,则最好在创建映像时自己将该信息存储在本地数据库中。这就是MediaStore对框架的作用。

答案 2 :(得分:0)

您可以使用RelativeLayoutImageViewTextView与日期合并,检查this answer