How to get the size of an external SD storage card (with an installed SD card)?

Link: I worked based on this link

I added this line to find the size (both internal and external),

return availableExternalMemorySize/(1024*1024); 

I tested my tablet. I get both the size of the internal and external SD card, as

Inside the internal storage:

  • Total memory - 1007
  • Available memory --683

In external storage:

  • Shared memory - 1763
  • Available memory - 1554

But in Tablet, I saw the settings. External storage size is 8 GB. But it shows me 1.7 GB when I tested programmatically.

What is the procedure for determining the size of external storage?

+9
android storage android-externalstorage sd-card
May 30 '13 at 11:28
source share
3 answers

In order to have β€œfree” space on the external SD card, to display a number that is consistent with the menu-> Settings-> SD card and phone number, use the following code:

 StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); double sdAvailSize = (double)stat.getAvailableBlocks() * (double)stat.getBlockSize(); //One binary gigabyte equals 1,073,741,824 bytes. double gigaAvailable = sdAvailSize / 1073741824; 

Here's how you get the 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's how you get the 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; 



Brief note

Free blocks:

The total number of blocks that are free on the file system, including reserved blocks (which are not available for regular applications).

Available blocks:

The number of blocks free from the file system and available to the application.




Here's how to determine if an SD card is installed:

  String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { // We can read and write the media } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // We can only read the media } else { // No external media } 

Related documentation: http://developer.android.com/reference/android/os/StatFs.html

+13
May 30 '13 at 11:40
source share

You can use getTotalSpace () , getFreeSpace () or getUsableSpace () https://developer.android.com/reference/java/io/File.html

 import java.io.File; import android.util.Log; .... File f = getMyFile(); Log.d("MyApp", f.getTotalSpace()+""); .... 
+1
Apr 26 '16 at 13:34
source share

To allow for outdated and cross-availability, I made these methods.

 public static long sdCardFree_bytes() { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long free_memory = 0; //return value is in bytes if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) { free_memory = stat.getAvailableBlocksLong() * stat.getBlockSizeLong(); } else { free_memory = stat.getAvailableBlocks() * stat.getBlockSize(); } return free_memory; } public static long sdCardUsed_bytes() { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long free_memory = 0; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) { free_memory = (stat.getBlockCountLong() - stat.getAvailableBlocksLong()) * stat.getBlockSizeLong(); //return value is in bytes } else { free_memory = (stat.getBlockCount() - stat.getAvailableBlocks()) * stat.getBlockSize(); //return value is in bytes } return free_memory; } public static long sdCardTotal_bytes() { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long free_memory = 0; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) { free_memory = stat.getBlockCountLong() * stat.getBlockSizeLong(); //return value is in bytes } else { free_memory = stat.getBlockCount() * stat.getBlockSize(); //return value is in bytes } return free_memory; } 
0
Nov 30 '15 at 15:09
source share



All Articles