How we can handle the file saving on low device memory(internal/Excternal memrory). I know if sufficient space is not available the OS will throw IOException but is there any way to handle this gracefully.
Asked
Active
Viewed 1,515 times
5
-
1Before writing file content, just check for the available free space. – Lucifer Apr 18 '13 at 06:43
-
As @Lucifer commented ,after checking for available size if u find that the available size is less than the file size then you can use some compression algorithms to compress your file ,but remember compression algorithms takes time – Abx Apr 18 '13 at 06:50
-
see http://stackoverflow.com/q/3394765/2147039 , http://stackoverflow.com/q/2941552/2147039 – Lucifer Apr 18 '13 at 06:55
3 Answers
1
First find available space of internal and external memory space.. then after check your file size is less then available memory (choice internal or external as per your requirement) then store otherwise message alert display... if you need format size then use function of formatSize....
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 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 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();
}
Thanks

SBJ
- 4,089
- 3
- 24
- 27
1
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(this, availableBlocks * blockSize);

Arun C
- 9,035
- 2
- 28
- 42
1
You try out the below function for checking the internal memory availability.
/** * This function find outs the free space for the given path. * * @return Bytes. Number of free space in bytes. */ public static long getFreeSpace() { try { if (Environment.getExternalStorageDirectory() != null && Environment.getExternalStorageDirectory().getPath() != null) { StatFs m_stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); long m_blockSize = m_stat.getBlockSize(); long m_availableBlocks = m_stat.getAvailableBlocks(); return (m_availableBlocks * m_blockSize); } else { return 0; } } catch (Exception e) { e.printStackTrace(); return 0; } }
Use the above function as belo :
int m_fileSizeInKB = m_fileSize / 1024; if (m_fileSize >= getFreeSpace()) { //Show the message to user that there is not enough space available in device. } else { //your business logic here. }

GrIsHu
- 29,068
- 10
- 64
- 102
-
Hi Guys, Very thankful for reply. I have one more query if the file size is not known before saving file. Then how can we handle low memory scenario. Is there any need to handle the same by checking filespace before saving file or catch IOException. What is best practice for some data centric applications? – Sumit Gulati Apr 18 '13 at 09:34
-
You can check the file size before saving it into memory that is the best practice. – GrIsHu Apr 18 '13 at 11:18
-