如何获取Android SD卡上的可用空间?

发布于 2024-09-18 19:35:41 字数 126 浏览 3 评论 0原文

我正在使用 Environment.getExternalStorageDirectory() 创建文件,在创建和存储文件之前我需要知道是否有足够的可用空间。

如果您可以引用 Android Ref 文档,那也会很有帮助。

I am using Environment.getExternalStorageDirectory() to create a file and I will need to know if there is enough space available before I create and store the file.

If you can cite the Android Ref doc, that would be helpful too.

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

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

发布评论

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

评论(4

黑色毁心梦 2024-09-25 19:35:41

您必须使用 StatFs 类。以下是如何使用它的示例,该示例取自手机上“设置”应用程序的源代码。

        if (status.equals(Environment.MEDIA_MOUNTED)) {
        try {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long totalBlocks = stat.getBlockCount();
            long availableBlocks = stat.getAvailableBlocks();

            mSdSize.setSummary(formatSize(totalBlocks * blockSize));
            mSdAvail.setSummary(formatSize(availableBlocks * blockSize) + readOnly);

            mSdMountToggle.setEnabled(true);
            mSdMountToggle.setTitle(mRes.getString(R.string.sd_eject));
            mSdMountToggle.setSummary(mRes.getString(R.string.sd_eject_summary));

        } catch (IllegalArgumentException e) {
            // this can occur if the SD card is removed, but we haven't received the 
            // ACTION_MEDIA_REMOVED Intent yet.
            status = Environment.MEDIA_REMOVED;
        }

You have to use the StatFs Class. Here is an example of how to use it taken from the source code to the Settings application found on the phone.

        if (status.equals(Environment.MEDIA_MOUNTED)) {
        try {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long totalBlocks = stat.getBlockCount();
            long availableBlocks = stat.getAvailableBlocks();

            mSdSize.setSummary(formatSize(totalBlocks * blockSize));
            mSdAvail.setSummary(formatSize(availableBlocks * blockSize) + readOnly);

            mSdMountToggle.setEnabled(true);
            mSdMountToggle.setTitle(mRes.getString(R.string.sd_eject));
            mSdMountToggle.setSummary(mRes.getString(R.string.sd_eject_summary));

        } catch (IllegalArgumentException e) {
            // this can occur if the SD card is removed, but we haven't received the 
            // ACTION_MEDIA_REMOVED Intent yet.
            status = Environment.MEDIA_REMOVED;
        }
濫情▎り 2024-09-25 19:35:41

使用 StatFS。如果您将 Environment.getExternalStorageDirectory() 中的值传递给构造函数(当然,转换为 String),它应该可以工作。

Use StatFS. It should work if you pass in the value from Environment.getExternalStorageDirectory() to the constructor (converting to String, of course).

逆光下的微笑 2024-09-25 19:35:41

您可能需要重新设置才能获得准确的结果:

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
stat.restat(Environment.getExternalStorageDirectory().getAbsolutePath());
long available = ((long) stat.getAvailableBlocks() * (long) stat.getBlockSize());

You may have to restat to get accurate results:

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
stat.restat(Environment.getExternalStorageDirectory().getAbsolutePath());
long available = ((long) stat.getAvailableBlocks() * (long) stat.getBlockSize());
┾廆蒐ゝ 2024-09-25 19:35:41

自 API 级别 9 起可用:

Environment.getExternalStorageDirectory().getUsableSpace();

http://developer.android .com/reference/java/io/File.html#getUsableSpace()

Usable since API level 9:

Environment.getExternalStorageDirectory().getUsableSpace();

http://developer.android.com/reference/java/io/File.html#getUsableSpace()

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