I am trying to figure out the available disk space on the Android phone running my application. Is there a way to do this programmatically?
Thanks,
File pathOS = Environment.getRootDirectory();//Os Storage
StatFs statOS = new StatFs(pathOS.getPath());
File pathInternal = Environment.getDataDirectory();// Internal Storage
StatFs statInternal = new StatFs(pathInternal.getPath());
File pathSdcard = Environment.getExternalStorageDirectory();//External(SD CARD) Storage
StatFs statSdcard = new StatFs(pathSdcard.getPath());
if((android.os.Build.VERSION.SDK_INT < 18)) {
// Get Android OS (system partition) free space API 18 & Below
int totalBlocksOS = statOS.getBlockCount();
int blockSizeOS = statOS.getBlockSize();
int availBlocksOS = statOS.getAvailableBlocks();
long total_OS_memory = (long) totalBlocksOS * (long) blockSizeOS;
long free_OS_memory = (long) availBlocksOS * (long) blockSizeOS;
long Used_OS_memory = total_OS_memory - free_OS_memory;
TotalOsMemory = total_OS_memory ;
FreeOsMemory = free_OS_memory;
UsedOsMemory = Used_OS_memory;
// Get internal (data partition) free space API 18 & Below
int totalBlocksInternal = statInternal.getBlockCount();
int blockSizeInternal = statOS.getBlockSize();
int availBlocksInternal = statInternal.getAvailableBlocks();
long total_Internal_memory = (long) totalBlocksInternal * (long) blockSizeInternal;
long free_Internal_memory = (long) availBlocksInternal * (long) blockSizeInternal;
long Used_Intenal_memory = total_Internal_memory - free_Internal_memory;
TotalInternalMemory = total_Internal_memory;
FreeInternalMemory = free_Internal_memory ;
UsedInternalMemory = Used_Intenal_memory ;
// Get external (SDCARD) free space for API 18 & Below
int totalBlocksSdcard = statSdcard.getBlockCount();
int blockSizeSdcard = statOS.getBlockSize();
int availBlocksSdcard = statSdcard.getAvailableBlocks();
long total_Sdcard_memory = (long) totalBlocksSdcard * (long) blockSizeSdcard;
long free_Sdcard_memory = (long) availBlocksSdcard * (long) blockSizeSdcard;
long Used_Sdcard_memory = total_Sdcard_memory - free_Sdcard_memory;
TotalSdcardMemory = total_Sdcard_memory;
FreeSdcardMemory = free_Sdcard_memory;
UsedSdcardMemory = Used_Sdcard_memory;
}
else {
// Get Android OS (system partition) free space for API 18 & Above
long total_OS_memory = (statOS. getBlockCountLong() * statOS.getBlockSizeLong());
long free_OS_memory = (statOS. getAvailableBlocksLong() * statOS.getBlockSizeLong());
long Used_OS_memory = total_OS_memory - free_OS_memory;
TotalOsMemory = total_OS_memory ;
FreeOsMemory = free_OS_memory;
UsedOsMemory = Used_OS_memory;
// Get internal (data partition) free space for API 18 & Above
long total_Internal_memory = (statInternal. getBlockCountLong() * statInternal.getBlockSizeLong());
long free_Internal_memory = (statInternal. getAvailableBlocksLong() * statInternal.getBlockSizeLong());
long Used_Intenal_memory = total_Internal_memory - free_Internal_memory;
TotalInternalMemory = total_Internal_memory;
FreeInternalMemory = free_Internal_memory ;
UsedInternalMemory = Used_Intenal_memory ;
// Get external (SDCARD) free space for API 18 & Above
long total_Sdcard_memory = (statSdcard. getBlockCountLong() * statSdcard.getBlockSizeLong());
long free_Sdcard_memory = (statSdcard. getAvailableBlocksLong() * statSdcard.getBlockSizeLong());
long Used_Sdcard_memory = tota*emphasized text*l_Sdcard_memory - free_Sdcard_memory;
TotalSdcardMemory = total_Sdcard_memory;
FreeSdcardMemory = free_Sdcard_memory;
UsedSdcardMemory = Used_Sdcard_memory;
}
}