I don't know if this qualifies as a "non-hack" to you, but if you don't want to reinvent the wheel, you could just use the Linux command du
. Here's a clip from its manpage:
NAME
du - estimate file space usage
SYNOPSIS
du [OPTION]... [FILE]...
DESCRIPTION
Summarize disk usage of each FILE, recursively for directories.
In particular the parameters -c
and -s
should interest you:
$ du -sc /tmp
164 /tmp
164 total
$
The number it outputs is the total number of bytes in the directory. I don't know if you want your size in bytes or human-readable format, but -h
is there for you if you need that too.
You'll have to read the output from the command. Capturing command output has already been covered in this question, from which I'm going to borrow heavily to provide the following example:
public String du(String fileName) {
Class<?> execClass = Class.forName("android.os.Exec");
Method createSubprocess = execClass.getMethod("createSubprocess", String.class, String.class, String.class, int[].class);
int[] pid = new int[1];
FileDescriptor fd = (FileDescriptor)createSubprocess.invoke(null, "/system/bin/du -sc", fileName, null, pid);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fd)));
String output = "";
try {
String line;
while ((line = reader.readLine()) != null) {
output += line + "\n";
}
}
catch (IOException e) {}
return output;
}
From there you would need to parse the output for the numerical value representing the total size, which I'm leaving out as it should be fairly trivial. Optionally, you could just throw that in the du()
function and make the function return an int
instead of a String
.