- import java.io.File;
- import android.os.Environment;
- import android.os.StatFs;
- File path = Environment.getExternalStorageDirectory();
- StatFs stat = new
StatFs(path.getPath()); //或者直接给出路径:StatFs stat = new StatFs("/media/C");
- long blockSize = stat.getBlockSize(); //每个块的大小
- long
totalBlocks = stat.getBlockCount(); //块的数目
- long availableBlocks = stat.getAvailableBlocks(); //可用块的数目
StatFs类在frameworks/base/core/java/android/os/StaFs.java中定义。其成员函数会调用native层的函数,
native层的函数在frameworks/base/core/jni/android_os_StatFs.cpp中定义。通过以下函数注册:
int register_android_os_StatFs(JNIEnv *env)
{
}
其中StatFs的构造函数会去调用native层的函数,native层的函数又会去调用kernel中的函数statfs,进而去调用驱动中的函数。
Environment类在frameworks/base/core/java/android/os/Environment.java中定义。
其中在camera的应用中,存在一个ImageManager类,其在在ImageManger.java中定义,其中有一成员函数检查文件夹的可读性:
private static boolean checkFsWritable() {
// Create a temporary file to see whether a volume is really writeable.
// It's important not to put it in the root directory which may have a
// limit on the number of files.
String directoryName =
Environment.getExternalStorageDirectory().toString() + "/DCIM"; // "/sdcard/DCIM"
File directory = new File(directoryName);
if (!directory.isDirectory()) { //检查是否存在该文件夹
if (!directory.mkdirs()) { //建立该文件夹
return false;
}
}
return directory.canWrite(); //返回该文件夹的可写属性
}