Search code examples
androidfile-manager

get internal and external storage information- android


i am working on a android file manager app. so on main activity i want to show the all the available storage types like internal storage and external sd card.

so i used this code,

public static boolean externalMemoryAvailable() {
    return android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED);
}

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

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

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

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

but the problem is, its giving me same memory outputs for both internal and external storage.. actually its giving right answer for internal storage. but wrong for external sd card.

i think i am wrong at getting the path of ext sd card. any help? plz.


Solution

  • Yes the sd card location path is different for different makes of android and cannot be guaranteed. I have a solution but this works with minSdkVersion 19.

    static  File dirs[];
    dirs = ContextCompat.getExternalFilesDirs(context, null);
    //dirs[0] refers to internal memory and dirs[1] gives you external. Call the following methods to get total and available memory details.
    
    
    public static String getTotalExternalMemorySize(File dirs[]) {
    if (dirs.length > 1) {
            StatFs stat = new StatFs(dirs[1].getPath());
            long blockSize = stat.getBlockSizeLong();
            long totalBlocks = stat.getBlockCountLong();
            return readableFileSize(totalBlocks * blockSize);
        } else {
            return "NA";
    }
    
     public static String getAvailableExternalMemorySize(File[] dirs) {
        if (dirs.length > 1) {
            StatFs stat = new StatFs(dirs[1].getPath());
            long blockSize = stat.getBlockSizeLong();
            long availableBlocks = stat.getAvailableBlocksLong();
            return readableFileSize(availableBlocks * blockSize);
        } else {
            return "NA";
        }
    }
    
     public static String readableFileSize(long size) {
        if(size <= 0) return "0";
        final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" };
        int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
        return new DecimalFormat("#,##0.##").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }