如何正确获取android.net.Uri的文件大小? [英] How to correctly get the file size of an android.net.Uri?

查看:160
本文介绍了如何正确获取android.net.Uri的文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个android.net.Uri指向我需要确定文件大小的文件。目前这是使用contentResolver上的查询完成的(我使用的是Xamarin,所以它是C#):

I have an android.net.Uri pointing to a file of which I need to determine the file size. Currently this is done using a query on contentResolver (I am using Xamarin, so it's C#):

var cursor = contentResolver.Query(uri, new string[] {
    Android.Provider.OpenableColumns.Size,
    Android.Provider.OpenableColumns.DisplayName 
}, null, null, null);
cursor.MoveToFirst();
size = cursor.GetLong(0);
fileName = cursor.GetString(1);
cursor.Close();

这大部分时间都有效,但对于某些文件,该值似乎略大于正确的文件大小(我使用Total Commander作为参考)。

This works most of the time, but for some files, the value seems to be slightly bigger than the correct file size (I'm using Total Commander as a reference).

那里必须是一个更好的方法来做到这一点,我真的需要确切的大小信息,因为文件非常大,不能先将它们读入内存。为什么我的方法产生不正确的结果?

There must be a better way to do this, I really need the exact size information and, as the files are pretty large, can not read them into memory first. And why does my approach produce incorrect results?

提前感谢您的帮助。

推荐答案

虽然我没有找到查询返回错误结果的原因,但我发现另一种获取文件大小的方法似乎有效:

Although I did not find out why the query returns incorrect results, I found another way to get the file size which seems to work:

using (var fd = contentResolver.OpenFileDescriptor(uri, "r"))
    size = fd.StatSize;

这篇关于如何正确获取android.net.Uri的文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆