我需要获取外部和内部存储目录路径来查找它的大小,但我无法获取路径。在android中,我们有
android.os.Enviroment.getExternalStorageDirectory()
我需要获取外部和内部存储目录路径来查找它的大小,但我无法获取路径。在android中,我们有
android.os.Enviroment.getExternalStorageDirectory()
您可以在项目中创建一个 utils 类,并使用以下函数获取内部和外部存储路径:
/**
* Returns the absolute path to the directory of the device's internal storage
*
* @param context
* @return
*/
public static File getInternalStorage(Context context) {
return context.getFilesDir(); //Can be called directly too
}
/**
* Returns the absolute path to the directory of the device's primary shared/external storage
*
* @param context
* @return
*/
public static File getExternalStorage(Context context) {
File externalFilesDirPath = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
String externalStoragePath = "";
int subPathIndex = externalFilesDirPath.getAbsolutePath().indexOf("/emulated/0/");
if (subPathIndex > 0) {
subPathIndex += "/emulated/0/".length();
}
if (subPathIndex >= 0 && externalFilesDirPath.getAbsolutePath().contains("/storage/")) {
externalStoragePath = externalFilesDirPath.getAbsolutePath().substring(0, subPathIndex);
}
if (externalStoragePath.length() > 0) {
externalFilesDirPath = new File(externalStoragePath);
}
return externalFilesDirPath;
}
获取File对象后,您可以调用以下函数来获取存储信息 -
getTotalSpace()
:返回由此抽象路径名命名的分区的大小。getUsableSpace()
:返回此抽象路径名命名的分区上此虚拟机可用的字节数。getFreeSpace()
:返回此抽象路径名命名的分区中未分配的字节数。