如何在Kotlin中找到文件的大小-Android Studio

发布于 2025-01-30 05:47:41 字数 107 浏览 4 评论 0原文

我试图在Kotlin中找到文件的大小, 使用路径。文件尺寸。它正在崩溃,未发现的文件异常。

file.getName正在工作,正在为我提供文件名。

任何线索都将不胜感激。

I was trying to find the size of the file in kotlin,
using path.file size . It is crashing with file not found exception.

file.getname is working find and is giving me the file name.

any leads would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揪着可爱 2025-02-06 05:47:41

我们这些扩展功能集以获得大小。

val File.size get() = if (!exists()) 0.0 else length().toDouble()
val File.sizeInKb get() = size / 1024
val File.sizeInMb get() = sizeInKb / 1024
val File.sizeInGb get() = sizeInMb / 1024
val File.sizeInTb get() = sizeInGb / 1024

参考:所以问题

Us these set of extension functions to get the size.

val File.size get() = if (!exists()) 0.0 else length().toDouble()
val File.sizeInKb get() = size / 1024
val File.sizeInMb get() = sizeInKb / 1024
val File.sizeInGb get() = sizeInMb / 1024
val File.sizeInTb get() = sizeInGb / 1024

Ref: SO Question

再可℃爱ぅ一点好了 2025-02-06 05:47:41

获取文件大小的方法有不同的方法,您可以实现自己的扩展方法,另一种方法是

 Formatter.formatShortFileSize(context, File(absolutePath).length())

There are different ways to get the file size you can implement your own extension method and other way is

 Formatter.formatShortFileSize(context, File(absolutePath).length())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文