如何检查外部存储空间的可用性?

发布于 2024-09-08 02:47:18 字数 65 浏览 9 评论 0原文

如何检查 SD 卡是否已满,以便您的应用程序可以决定是否可以继续执行其工作,即写入外部存储或通知用户存储空间已用完。

How do you check if the SD card is full or not so that your application can decide if it can continue to do its job i.e. write to external storage or notify the user that storage has run out of space.

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

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

发布评论

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

评论(6

各自安好 2024-09-15 02:47:18

小心 StatFs &较新设备上的 int 溢出

此答案中的方法在具有大外部存储的设备上被破坏
例如,在我的 Nexus 7 上,它当前返回约 2 GB,而实际上还剩约 10 GB 空间。

// DOES NOT WORK CORRECTLY ON DEVICES WITH LARGE STORAGE DUE TO INT OVERFLOW
File externalStorageDir = Environment.getExternalStorageDirectory();
StatFs statFs = new StatFs(externalStorageDirectory.getAbsolutePath());  
int free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1024 / 1024;

StatFs 确实有返回 long 的替换方法,getAvailableBlocksLong()getBlockCountLong(),但问题是它们仅在API级别18中添加。

使用这个

最简单的方法是使用 getFreeSpace java.io.File中的 (),在 API 级别 9 中添加,返回 long

返回包含该分区的可用字节数
小路。如果该路径不存在,则返回 0。

因此,要获得外部存储(“SD 卡”)上的可用空间:

File externalStorageDir = Environment.getExternalStorageDirectory();
long free = externalStorageDir.getFreeSpace() / 1024 / 1024;

或者,如果您确实想要使用 StatFs 但需要支持 API 级别 18,这将修复整数溢出:

File externalStorageDir = Environment.getExternalStorageDirectory();
StatFs statFs = new StatFs(externalStorageDir.getAbsolutePath());  
long blocks = statFs.getAvailableBlocks();
long free = (blocks * statFs.getBlockSize()) / 1024 / 1024;

Watch out with StatFs & int overflow on newer devices

The approach in this answer is broken on devices with large external storage.
For example on my Nexus 7, it currently returns ~2 GB when in reality there is ~10 GB of space left.

// DOES NOT WORK CORRECTLY ON DEVICES WITH LARGE STORAGE DUE TO INT OVERFLOW
File externalStorageDir = Environment.getExternalStorageDirectory();
StatFs statFs = new StatFs(externalStorageDirectory.getAbsolutePath());  
int free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1024 / 1024;

StatFs does have replacement methods returning long, getAvailableBlocksLong() and getBlockCountLong(), but the problem is that they were only added in API level 18.

Use this instead

Simplest way is to use getFreeSpace() in java.io.File, added in API level 9, which returns long:

Returns the number of free bytes on the partition containing this
path. Returns 0 if this path does not exist.

So, to get free space on the external storage ("SD card"):

File externalStorageDir = Environment.getExternalStorageDirectory();
long free = externalStorageDir.getFreeSpace() / 1024 / 1024;

Alternatively, if you really want to use StatFs but need to support API level < 18, this would fix the integer overflow:

File externalStorageDir = Environment.getExternalStorageDirectory();
StatFs statFs = new StatFs(externalStorageDir.getAbsolutePath());  
long blocks = statFs.getAvailableBlocks();
long free = (blocks * statFs.getBlockSize()) / 1024 / 1024;
々眼睛长脚气 2024-09-15 02:47:18
/******************************************************************************************
Returns size in MegaBytes.

If you need calculate external memory, change this: 
   StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
to this: 
   StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());

******************************************************************************************/
    public long totalMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        long   total  = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
        return total;
    }

    public long freeMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
        long   free   = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
        return free;
    }

    public long busyMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        long   total  = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
        long   free   = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
        long   busy   = total - free;
        return busy;
    }
/******************************************************************************************
Returns size in MegaBytes.

If you need calculate external memory, change this: 
   StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
to this: 
   StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());

******************************************************************************************/
    public long totalMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        long   total  = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
        return total;
    }

    public long freeMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
        long   free   = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
        return free;
    }

    public long busyMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        long   total  = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
        long   free   = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
        long   busy   = total - free;
        return busy;
    }
所有深爱都是秘密 2024-09-15 02:47:18

使用StatFs并将外部存储目录的路径传递给构造函数,您可以调用getAvailableBlocks()getBlockSize()等函数StatFs 对象。

Use StatFs and pass the path of the external storage directory to the constructor and you can call functions such as getAvailableBlocks() and getBlockSize() on the StatFs object.

冷弦 2024-09-15 02:47:18

我认为你可以用这个陈述来解决你的问题。这无法检查SD卡的容量是否足够。

if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
    //to do something in here
}

I think you can use this statement to do with your problem. this cant check whether or no enough capacity of sdcard.

if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
    //to do something in here
}
走过海棠暮 2024-09-15 02:47:18
/**
 * @return Number of bytes available on external storage
 */
public static long getExternalStorageAvailableSpace() {
    long availableSpace = -1L;
    try {
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory()
                .getPath());
        stat.restat(Environment.getExternalStorageDirectory().getPath());
        availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return availableSpace;
}
/**
 * @return Number of bytes available on external storage
 */
public static long getExternalStorageAvailableSpace() {
    long availableSpace = -1L;
    try {
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory()
                .getPath());
        stat.restat(Environment.getExternalStorageDirectory().getPath());
        availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return availableSpace;
}
赤濁 2024-09-15 02:47:18

以下是获取内部存储大小的方法:

 StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());        
 long blockSize = statFs.getBlockSize();
 long totalSize = statFs.getBlockCount()*blockSize;
 long availableSize = statFs.getAvailableBlocks()*blockSize;
 long freeSize = statFs.getFreeBlocks()*blockSize;

以下是获取外部存储大小(SD 卡大小)的方法:

 StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());        
 long blockSize = statFs.getBlockSize();
 long totalSize = statFs.getBlockCount()*blockSize;
 long availableSize = statFs.getAvailableBlocks()*blockSize;
 long freeSize = statFs.getFreeBlocks()*blockSize;

Here is how you get internal storage sizes:

 StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());        
 long blockSize = statFs.getBlockSize();
 long totalSize = statFs.getBlockCount()*blockSize;
 long availableSize = statFs.getAvailableBlocks()*blockSize;
 long freeSize = statFs.getFreeBlocks()*blockSize;

Here is how you get external storage sizes (SD card size):

 StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());        
 long blockSize = statFs.getBlockSize();
 long totalSize = statFs.getBlockCount()*blockSize;
 long availableSize = statFs.getAvailableBlocks()*blockSize;
 long freeSize = statFs.getFreeBlocks()*blockSize;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文