I am making an android app which saves a 2MB .txt
file in the internal storage. How can I know if the internal storage is full, and thus save the file to the external storage?
Asked
Active
Viewed 1,027 times
1

vergil corleone
- 1,091
- 3
- 16
- 34
-
check exception if not enough space – Iftikar Urrhman Khan May 23 '13 at 09:28
3 Answers
3
After the quick search I found the following way. You can get it from StatFs class. This class is used to Retrieve overall information about the space on a filesystem.
public int FreeMemory()
{
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
return Free;
}
Also you can find by the following way
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(this, availableBlocks * blockSize);
You can get more detail from here and here too.
I hope this will help you. Thank you.

Community
- 1
- 1

Gunaseelan
- 14,415
- 11
- 80
- 128
1
Pleae check the following code (I have copied it in this site but I do not remember where exactly it is)
public static boolean externalMemoryAvailable() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
public static String getAvailableInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return formatSize(availableBlocks * blockSize);
}
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() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return formatSize(availableBlocks * blockSize);
} else {
return ERROR;
}
}
public static String getTotalExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return formatSize(totalBlocks * blockSize);
} else {
return ERROR;
}
}
public static String formatSize(long size) {
String suffix = null;
if (size >= 1024) {
suffix = "KB";
size /= 1024;
if (size >= 1024) {
suffix = "MB";
size /= 1024;
}
}
StringBuilder resultBuffer = new StringBuilder(Long.toString(size));
int commaOffset = resultBuffer.length() - 3;
while (commaOffset > 0) {
resultBuffer.insert(commaOffset, ',');
commaOffset -= 3;
}
if (suffix != null) resultBuffer.append(suffix);
return resultBuffer.toString();
}
wish to help you.

RealDream
- 407
- 3
- 6
- 18
0
have you tried float freeSpace = DeviceMemory.getInternalFreeSpace();
?

Basim Sherif
- 5,384
- 7
- 48
- 90