gogoWebsite

Android obtains the internal storage space and external storage space of the phone

Updated to 2 days ago

import java . io . File ;                                                                                                
import android . os . Environment ;    
import android . os . StatFs ;    
    
public class StorageUtil {

    private static final int ERROR = - 1 ;

    /**
* Is SDCARD stored?
     */
    public static boolean externalMemoryAvailable () {
        return android . os . Environment . getExternalStorageState (). equals (
                android . os . Environment . MEDIA_MOUNTED );
    }

    /**
* Get the remaining storage space inside the phone
     * @return
     */
    public static long getAvailableInternalMemorySize () {
        File path = Environment . getDataDirectory ();
        StatFs stat = new StatFs ( path . getPath ());
        long blockSize = stat . getBlockSize ();
        long availableBlocks = stat . getAvailableBlocks ();
        return availableBlocks * blockSize ;
    }

    /**
* Get the total storage space inside the phone
     * @return
     */
    public static long getTotalInternalMemorySize () {
        File path = Environment . getDataDirectory ();
        StatFs stat = new StatFs ( path . getPath ());
        long blockSize = stat . getBlockSize ();
        long totalBlocks = stat . getBlockCount ();
        return totalBlocks * blockSize ;
    }

    /**
* Get the remaining storage space of SDCARD
     * @return
     */
    public static long getAvailableExternalMemorySize () {
        if ( externalMemoryAvailable ()) {
            File path = Environment . getExternalStorageDirectory ();
            StatFs stat = new StatFs ( path . getPath ());
            long blockSize = stat . getBlockSize ();
            long availableBlocks = stat . getAvailableBlocks ();
            return availableBlocks * blockSize ;
        } else {
            return ERROR ;
        }
    }

    /**
* Get the total storage space of SDCARD
     * @return
     */
    public static long getTotalExternalMemorySize () {
        if ( externalMemoryAvailable ()) {
            File path = Environment . getExternalStorageDirectory ();
            StatFs stat = new StatFs ( path . getPath ());
            long blockSize = stat . getBlockSize ();
            long totalBlocks = stat . getBlockCount ();
            return totalBlocks * blockSize ;
        } else {
            return ERROR ;
        }
    }
}