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
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
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;
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = stat.getBlockSizeLong() * stat.getBlockCountLong();
long megAvailable = bytesAvailable / 1048576;
Also, use this for internal size.
StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
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.
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(":");
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)
© 2022 - 2024 — McMap. All rights reserved.