How to check availability of space on external storage?

≡放荡痞女 提交于 2019-11-30 22:15:26

问题


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.


回答1:


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;



回答2:


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



回答3:


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.




回答4:


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
}



回答5:


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



回答6:


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;


来源:https://stackoverflow.com/questions/3163045/how-to-check-availability-of-space-on-external-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!