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

时间:2010-07-02 03:09:50

标签: android android-sdcard android-external-storage

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

6 个答案:

答案 0 :(得分:14)

注意StatFs& int在新设备上溢出

this answer 中的方法在具有大量外部存储空间的设备上被破坏。 例如,在我的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;

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

改为使用

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

  

返回包含此字节的分区上的空闲字节数   路径。如果此路径不存在,则返回0。

因此,要在外部存储空间获得可用空间(" SD卡"):

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

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

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

答案 1 :(得分:12)

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

答案 2 :(得分:8)

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

答案 3 :(得分:4)

我认为您可以使用此声明来处理您的问题。这不能检查是否有足够的SD卡容量。

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

答案 4 :(得分:0)

/**
 * @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;
}

答案 5 :(得分:0)

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

 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;