【问题标题】:Query information about a file android查询文件android的信息
【发布时间】:2011-04-18 20:40:55
【问题描述】:

对于 content: URI,我使用 ContentProvider/Contentresolver 来检索有关修改日期、大小和数据的信息。我想对 file: URI 做同样的事情,但是当我尝试以下代码时:

CursorLoader loader = new CursorLoader(this, uri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();

光标返回为空。如何获取文件大小、数据和添加/修改日期?

【问题讨论】:

    标签: android file cursor


    【解决方案1】:

    我可能误解了你的问题,但你使用 java 的普通文件 IO 来查找该信息。

    这是我为名为 cat.jpg 的图片查找该信息的示例:

    File f = new File("/data/cat.jpg");
    
    //Size of file in bytes
    Long size = f.length();
    
    //Time of when the file was last modified in microseconds
    Long lastModified = f.lastModified();
    
    //The bytes of the file. This gets populated below
    byte[] fileData = new byte[(int)f.length()];
    
    try {
        FileInputStream fis = new FileInputStream(f);
    
        int offset = 0;
        int bytesActuallyRead;
    
        while( (bytesActuallyRead = fis.read(fileData, offset, 1024)) > 0) {
            offset += bytesActuallyRead;
        }
    
    } catch(Exception e) {
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-03
      • 2010-09-08
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多