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);
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);
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.
Checking if the storage is full before writing to internal storage
You have asked several questions, but all you need to do is a simple check to see if there is available storage memory before proceeding with any file write operation.
You can use this utility method to quickly check for free storage space on the device,
public static long getAvailableInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize, availableBlocks;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
blockSize = stat.getBlockSizeLong();
availableBlocks = stat.getAvailableBlocksLong();
} else {
blockSize = stat.getBlockSize();
availableBlocks = stat.getAvailableBlocks();
}
return availableBlocks * blockSize;
}
As you know you can get the size of your files using the length()
method of the files. It will return you the size of each file in bytes.
All you need to do is compare the size of the files you intend to write with the available storage memory. If the size of your files is less than the total storage available, you can go ahead and write your files.
Retrieve free internal memory on an Android device
Try this. following code returns Internal Storage Memory in MB ,
StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
long bytesAvailable = (long)stat.getFreeBlocks() * (long)stat.getBlockSize();
long megAvailable = bytesAvailable / 1048576;
How to monitor android device free space programatically
The contents of getRootDirectory()
is unlikely to change, since that partition is read-only.
If you want to monitor free space, you have to choose a file on the partition that matters to you, such as getFilesDir()
to find out the free space on internal storage.
Related Topics
Android Action Bar Not Showing Overflow
Launch Android Application Without Main Activity and Start Service on Launching Application
Create and Share a File from Internal Storage
Realmchangelistener Does Not Get Called When Realm Gets Updated in Notificationlistenerservice
Android Broadcast Receiver Bluetooth Events Catching
First Launch Take Long Time (Classloader Referenced Unknown Path)
Android Java Lang Runtimeexception Fail to Connect to Camera Service
How to Send Multiple Sms to Single Number
Which Correct Flag of Autoconnect in Connectgatt of Ble
How to Determine the Screen Width in Terms of Dp or Dip at Runtime in Android
Android: How to Make the Keypad Always Visible
Android: Http Communication Should Use "Accept-Encoding: Gzip"
Firebase Overwrites Signin with Google Account
Add a Background Image to Shape in Xml Android
Action_Sendto for Sending an Email