1

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.

varsha valanju
  • 801
  • 1
  • 9
  • 27
  • `but wrong for external sd card`. Indeed. There is nothing in your code that refers to an external SD card. – greenapps Feb 17 '17 at 12:51
  • `its giving me same memory outputs for both internal and external storage..`. Interesting. Confirmed! – greenapps Feb 17 '17 at 13:00

2 Answers2

2

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];
}
Pavan
  • 767
  • 4
  • 18
  • thanx @pavan. but will you plz tell me, how can i detect another usb storage. means what if the phone has connected usb pen drive with otg. then how can i detect it here? – varsha valanju Feb 17 '17 at 13:25
  • means what to if, the phone has sd card inserted, as well as pendrive connected? – varsha valanju Feb 17 '17 at 13:25
  • Did you try to loop through dirs[] array to check if it returned all the external mounts ? According to the documentation **getExternalFilesDirs(context, type)** method does the following : **Returns absolute paths to application-specific directories on all external storage devices where the application can place persistent files it owns** – Pavan Feb 18 '17 at 02:55
  • np. I have also updated the answer with readableFileSize method in case you needed it. – Pavan Feb 18 '17 at 04:36
0

Don't forget to set the permission for external storage <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

See also Accessing getExternalStorageDirectory

Community
  • 1
  • 1
Fusselchen
  • 382
  • 1
  • 4
  • 12