It is possible to get total storage capacity?
Asked Answered
S

5

7

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

Sheepish answered 25/1, 2013 at 23:13 Comment(2)
so like some sort of function, that adds this two values together... ;-)Tarrah
you didn't understand me, i want max capacity of internal, and max capacity for external, i dont want the sum of internal + external....Sheepish
D
7

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;
Discredit answered 14/8, 2013 at 15:55 Comment(1)
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 MBAleenaleetha
P
5
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = stat.getBlockSizeLong() * stat.getBlockCountLong();
long megAvailable = bytesAvailable / 1048576;

Source

Also, use this for internal size.

StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
Polyphagia answered 25/1, 2013 at 23:15 Comment(0)
M
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.

Menorca answered 22/10, 2017 at 22:20 Comment(0)
B
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(":");
Bullington answered 1/4, 2018 at 23:30 Comment(0)
A
0
    val stat = StatFs(Environment.getExternalStorageDirectory().path)

    val available = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        stat.blockCountLong * stat.blockSizeLong
    } else {
        stat.blockCount.toLong() * stat.blockSize.toLong()
    }
    val totalInMB = bytesAvailable / (1024*1024)
Alisun answered 24/3, 2023 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.