开发者

Query information about a file android

For a content: URI, I used ContentProvider/Contentresolver to retrieve information about the modified date, size and data. I would like to do the same for file: URI but when I try the following code:

CursorLoader loader = new CursorLoader(this, ur开发者_开发技巧i, proj, null, null, null);
Cursor cursor = loader.loadInBackground();

Cursor is returned as null. How to get the file size, data and date added/modified?


I might be misunderstanding your question, but you use java's normal file IO to find that information.

Here is an example of me finding out that information for a picture called 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) {

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜