简体   繁体   English

无法从 Uri 获取上次修改日期

[英]Can't get last modified date from Uri

I have some files saved in the private app-specific folder of the app.我有一些文件保存在应用程序的专用应用程序专用文件夹中。

If I get a file uri through the FileProvider如果我通过 FileProvider 获得文件 uri

val fileUri = getUriForFile(this, "$packageName.fileprovider", file)

and query the content resolver to get the file metadata并查询内容解析器以获取文件元数据

contentResolver.query(fileUri, null, null, null, null)

and check all the columnNames inside the cursor I'm getting, I can see that there are only two columns, which are _display_name and _size并检查我得到的 cursor 中的所有列名,我可以看到只有两列,分别是_display_name_size

What if I wanted to obtain the date of creation/last modification of the file from the uri?如果我想从 uri 中获取文件的创建/最后修改日期怎么办? Is there something I could do?有什么我可以做的吗?

(I know I could get it from the File object, but I'd need it from the uri, the real code is more complex than this example.) (我知道我可以从文件 object 中获取它,但我需要从 uri 中获取它,实际代码比这个示例更复杂。)

Thanks!谢谢!

What if I wanted to obtain the date of creation/last modification of the file from the uri?如果我想从 uri 中获取文件的创建/最后修改日期怎么办? Is there something I could do?有什么我可以做的吗?

That depends a bit on your overall setup.这在一定程度上取决于您的整体设置。

There's no way to get that information for an arbitrary Uri .无法获取任意Uri的信息。 There's no requirement for, say, Google Drive to provide that information, which is why it's not part of the OpenableColumns protocol that you're using (or anything else).例如,Google Drive 不需要提供该信息,这就是为什么它不是您正在使用的OpenableColumns协议(或其他任何东西)的一部分。 So, if the reason you're pushing away from using the File is because you have a mixed bag of Uri values from various sources, then you're probably in trouble.因此,如果您放弃使用File的原因是因为您拥有来自各种来源的混杂的Uri值,那么您可能遇到了麻烦。

If, OTOH, you know that your Uri values are always from your FileProvider , you can reverse-engineer the filesystem path from the Uri value, use that to construct a File , and get the info that way (or, at least, whatever Android makes available via those File methods).如果,OTOH,您知道您的Uri始终来自您的FileProvider ,您可以从Uri值对文件系统路径进行反向工程,使用它来构造File ,并以这种方式获取信息(或者,至少,无论 Android通过这些File方法可用)。 Or, if you can live with the creation/modification time information only being available for a subset of your Uri list, use the aforementioned hack for your own FileProvider Uri values and live without the data for Uri values that you get from elsewhere.或者,如果您可以忍受仅可用于Uri列表的子集的创建/修改时间信息,请对您自己的FileProvider Uri值使用上述技巧,而无需从其他地方获得的Uri值数据。

So I also wanted to have the same.所以我也想拥有同样的。 I use an Intent to let the user pick an image.我使用 Intent 让用户选择图像。 When a user selects an image from Camera I can get the last modified via:当用户从相机中选择图像时,我可以通过以下方式获得最后修改:

fun ContentResolver.lastModified(uri: Uri): Instant? {
  val query = query(uri, null, null, null, null)

  query?.use {
    if (it.moveToFirst()) {
      val index = it.getColumnIndex("last_modified")
      
      if (index >= 0) {
        return Instant.fromEpochMilliseconds(it.getLong(index))
      }
    }
  }
  
  return null
}

Note: This might not work for every app, however it's a good first start.注意:这可能不适用于每个应用程序,但它是一个很好的开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM