Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an External storage sd card size (With Mounted SD card)?

Link :I worked on based on this Link

I added this line to find the size (both Internal and External) size,

return availableExternalMemorySize/(1024*1024);

I tested in my Tablet. I am getting both Internal and External SD card size as,

In Internal Storage:

  1. Total Memory --1007
  2. Available Memory --683

In External Storage:

  1. Total Memory -- 1763
  2. Available Memory -- 1554

But in Tablet, I saw settings. An External Storage size has 8GB. But it is showing me 1.7 GB around when I tested via programmatically.

What is the procedure to find an External storage size?

like image 553
Achiever Avatar asked May 30 '13 11:05

Achiever


People also ask

Is SD card external storage?

Basically, there are two types of external storage. One is the popular form of external memory that most smartphone users understand i.e. SD card, also known as memory cards that is the secondary external storage and the other one built-in external storage which is known as primary external storage.


2 Answers

 Use the
following code
it may
help

public void getInternalmemorySize() {
    StatFs stat_fs = new StatFs(Environment.getExternalStorageDirectory().getPath());
    double avail_sd_space = (double) stat_fs.getAvailableBlocksLong() * (double) stat_fs.getBlockSizeLong();
    double GB_Available = (avail_sd_space / 1073741824);
    double GBTotal = ((double) stat_fs.getBlockCountLong() * (double) stat_fs.getBlockSizeLong()) / 1073741824;
    SDCardCheck();
    Log.e("Memory", "Available MB  Internal: " + GB_Available + "---" + GBTotal);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
static String getExternalSdCardSize() {
    File storage = new File("/storage");
    String external_storage_path = "";
    String size = "";

    if (storage.exists()) {
        File[] files = storage.listFiles();

        for (File file : files) {
            if (file.exists()) {
                try {
                    if (Environment.isExternalStorageRemovable(file)) {
                        // storage is removable
                        external_storage_path = file.getAbsolutePath();
                        break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("TAG", e.toString());
                }
            }
        }
    }

    if (!external_storage_path.isEmpty()) {
        File external_storage = new File(external_storage_path);
        if (external_storage.exists()) {
            size = totalSize(external_storage);
        }
    }
    return size;
}

private static String totalSize(File file) {
    StatFs stat = new StatFs(file.getPath());
    long blockSize, totalBlocks;
    long avaiblockSize, availableBlocks;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        blockSize = stat.getBlockSizeLong();
        totalBlocks = stat.getBlockCountLong();
        avaiblockSize = stat.getAvailableBlocksLong();
        availableBlocks = stat.getBlockSizeLong();
    } else {
        blockSize = stat.getBlockSize();
        totalBlocks = stat.getBlockCount();
        avaiblockSize = stat.getAvailableBlocks();
        availableBlocks = stat.getBlockSize();
    }
    Log.e("Memory", "Memory--external--" + (double) (blockSize * totalBlocks) / 1073741824 + "---" + (double) (avaiblockSize * availableBlocks) / 1073741824);
    return formatSize(totalBlocks * blockSize);
}

private static String formatSize(long size) {
    String suffix = null;

    if (size >= 1024) {
        suffix = "KB";
        size /= 1024;
        if (size >= 1024) {
            suffix = "MB";
            size /= 1024;
        }
    }
    size = size / 1024;
    StringBuilder resultBuilder = new StringBuilder(Long.toString(size));

    int commaOffset = resultBuilder.length() - 3;
    while (commaOffset > 0) {
        resultBuilder.insert(commaOffset, ',');
        commaOffset -= 3;
    }

    if (suffix != null)
        resultBuilder.append(suffix);
    return resultBuilder.toString();
}

Ther is
some calculation
behalf of
these Methods.

StructStatVfs[
f_bavail=81523,
f_bfree=81523,
f_blocks=242304,
f_bsize=32768,
f_favail=0,
f_ffree=0,
f_files=0,
f_flag=1038,
f_frsize=32768,
f_fsid=0,
f_namemax=1530
        ]

StructStatVfs[
f_bavail=1633375,
f_bfree=1641567,
f_blocks=3134770,
f_bsize=4096,
f_favail=767939,
f_ffree=767939,
f_files=804672,
f_flag=1038,
f_frsize=4096,
f_fsid=0,
f_namemax=255
        ]


Internal-

        3134770*4096/1024*1024*1024=11.957.10 1633375*4096/1024*1024*1024=6.23

External-
        81523*32768/1024*1024*1024=2.487 242304*32768/1024*1024*1024=7.39
like image 70
Rohit Sharma Avatar answered Oct 01 '22 01:10

Rohit Sharma


To get the external SD card's available "free" space to show a number which agrees with the Menu->Settings->SD card and phone storage's 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 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;

Short note

Free blocks:

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

Available blocks:

The number of blocks that are free on the file system and available to applications.


Here is how to detect whether SD card is mounted:

 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
 }

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

like image 32
MDMalik Avatar answered Oct 01 '22 02:10

MDMalik