Android: How to get a file's creation date?

后端 未结 6 1719
孤独总比滥情好
孤独总比滥情好 2020-11-28 05:38

This is my code:

File TempFiles = new File(Tempfilepath);
if (TempFiles.exists()) {
    String[] child = TempFiles.list();
    for (int i = 0; i < child.l         


        
6条回答
  •  温柔的废话
    2020-11-28 05:50

    Having backward compatibility in mind I would rather use the following:

    fun getLastModifiedTimeInMillis(file: File): Long? {
        return try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                getLastModifiedTimeFromBasicFileAttrs(file)
            } else {
                file.lastModified()
            }
        } catch (x: Exception) {
            x.printStackTrace()
            null
        }
    }
    
    @RequiresApi(Build.VERSION_CODES.O)
    private fun getLastModifiedTimeFromBasicFileAttrs(file: File): Long {
        val basicFileAttributes = Files.readAttributes(
            file.toPath(),
            BasicFileAttributes::class.java
        )
        return basicFileAttributes.creationTime().toMillis()
    }
    

    alternatively, if you are dealing with jpg, jpegs you can use ExifInterface

提交回复
热议问题