5

i know how to get free capacity on internal memory and on external storage.

But i want to know max capacity of internal memory and max capacity of external storage, but i can't find any info about it on google

it is possible to achieve it?

thanks

Pableras84
  • 1,155
  • 5
  • 18
  • 30

4 Answers4

6

Some new methods were introduced in API 18. This is the code I used to get total storage (internal + external).

private static final long KILOBYTE = 1024;

StatFs internalStatFs = new StatFs( Environment.getRootDirectory().getAbsolutePath() );
long internalTotal;
long internalFree;

StatFs externalStatFs = new StatFs( Environment.getExternalStorageDirectory().getAbsolutePath() );
long externalTotal;
long externalFree;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    internalTotal = ( internalStatFs.getBlockCountLong() * internalStatFs.getBlockSizeLong() ) / ( KILOBYTE * KILOBYTE );
    internalFree = ( internalStatFs.getAvailableBlocksLong() * internalStatFs.getBlockSizeLong() ) / ( KILOBYTE * KILOBYTE );
    externalTotal = ( externalStatFs.getBlockCountLong() * externalStatFs.getBlockSizeLong() ) / ( KILOBYTE * KILOBYTE );
    externalFree = ( externalStatFs.getAvailableBlocksLong() * externalStatFs.getBlockSizeLong() ) / ( KILOBYTE * KILOBYTE );
}
else {
    internalTotal = ( (long) internalStatFs.getBlockCount() * (long) internalStatFs.getBlockSize() ) / ( KILOBYTE * KILOBYTE );
    internalFree = ( (long) internalStatFs.getAvailableBlocks() * (long) internalStatFs.getBlockSize() ) / ( KILOBYTE * KILOBYTE );
    externalTotal = ( (long) externalStatFs.getBlockCount() * (long) externalStatFs.getBlockSize() ) / ( KILOBYTE * KILOBYTE );
    externalFree = ( (long) externalStatFs.getAvailableBlocks() * (long) externalStatFs.getBlockSize() ) / ( KILOBYTE * KILOBYTE );
}

long total = internalTotal + externalTotal;
long free = internalFree + externalFree;
long used = total - free;
James McCracken
  • 14,870
  • 5
  • 51
  • 61
  • i m using api level 16 and this function is giving me logcat 11-22 14:29:45.933: D/MainActivity(6092): getTotalInternalExternalMemorySize method called 11-22 14:29:45.933: D/MainActivity(6092): if block 11-22 14:29:45.933: D/MainActivity(6092): total Memory:-704 MB 11-22 14:29:45.933: D/MainActivity(6092): free Memory:-641 MB 11-22 14:29:45.933: D/MainActivity(6092): used Memory:-63 MB – Erum Nov 22 '14 at 09:34
4
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getBlockCount();
long megAvailable = bytesAvailable / 1048576;

Source

Also, use this for internal size.

StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
MrZander
  • 2,970
  • 1
  • 28
  • 47
0

This method will not work on all phones, most phones return the total amount of memory available to the user, less system memory. you might try using the 'df -h' linux command in a c++ method using the NDK, but some phone make that a system (su) command only. So the long and short of it is you can't.

user330844
  • 822
  • 1
  • 11
  • 12
0

StatFs - Retrieve overall information about the space on a filesystem. This is a wrapper for Unix statvfs().

From API level 18 we use

long getTotalBytes - The total number of bytes supported by the file system. 

For older API use

int getBlockCount ()
int getBlockSize ()

StatFs stat = new StatFs(**path**);
long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();

As path You use Environment.getExternalStorageDirectory(), please review this value. In devices with multiple shared/external storage directories, this directory represents the primary storage that the user will interact with.

You need use string values of path like this: "/mnt/external_sd/" "/mnt/extSdCard/"

You can retreive list of all /mnt/ devices and use one of this values.

File allMounted = new File("/mnt/");
if(allMounted.isDirectory()){ 
String[] dirs = allMounted.list();...}

or something like

var myfile=new Java.IO.File("storage/");
var listOfStorages=myfile.ListFiles();

or

String[] externals = System.getenv("SECONDARY_STORAGE").split(":");
Mark Martin
  • 356
  • 3
  • 8