如何获得在Android上的SD卡的可用空间? [英] How to get the available space on Android sdcard?

查看:164
本文介绍了如何获得在Android上的SD卡的可用空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Environment.getExternalStorageDirectory()来创建一个文件,我需要知道,如果之前我创建和存储的文件有足够的可用空间。

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.

如果你能举出Android的参考文档,这将是有益的了。

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

推荐答案

您必须使用的体statfs 类。下面是如何使用它的源$ C ​​$ C到手机上找到的设置应用程序所采取的一个例子。

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;
        }

这篇关于如何获得在Android上的SD卡的可用空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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