Android get free size of internal/external memory
This is the way I did it :
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable;
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
bytesAvailable = stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
}
else {
bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();
}
long megAvailable = bytesAvailable / (1024 * 1024);
Log.e("","Available MB : "+megAvailable);
Get free space on internal memory
this post might fit well to your question.
also check this thread. there is so much info here on SO.
googled a bit and here is the solution (found at android git)
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(this, availableBlocks * blockSize);
Get Internal and External Memory size in MarshMallow
In lolipop or marshmallo you must use
Environment.getExternalStorageDirectory().getAbsolutePath();
to get external storage path instead of System.getenv("SECONDARY_STORAGE");
Refer beolw code for get external and internal storage in >android 5.0
public static String getTotalInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return formatSize(totalBlocks * blockSize);
}
public static String getAvailableExternalMemorySize() {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return formatSize(availableBlocks * blockSize);
}
How to find the amount of free storage (disk space) left on Android?
Try StatFs.getAvailableBlocks. You'll need to convert the block count to KB with getBlockSize.
How to Check available space on android device ? on SD card?
Try this code:
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();
long megAvailable = bytesAvailable / 1048576;
System.out.println("Megs: " + megAvailable);
Update:
getBlockCount()
- return size of SD card;
getAvailableBlocks()
- return the number of blocks that are still accessible to normal programs (thanks Joe)
Related Topics
List of Android Permissions Normal Permissions and Dangerous Permissions in API 23
Handling Registration Id Changes in Google Cloud Messaging on Android
Android Studio - How to Increase Allocated Heap Size
How to Customize Snackbar's Layout
Android Intent for Twitter Application
Android Open and Save Files To/From Google Drive Sdk
Android Multiple Email Attachments Using Intent
Where Are Shared Preferences Stored
How to Fire Onlistitemclick in Listactivity with Buttons in List
Android Room - Simple Select Query - Cannot Access Database on the Main Thread
How to Handle a Lost Keystore Password in Android
Gradle: Execution Failed for Task ':Processdebugmanifest'
Steps to Create APK Expansion File
Default Firebaseapp Is Not Initialized
How to Add Stacktrace or Debug Option When Building Android Studio Project