我试图在我的应用程序中显示内部和外部(SD卡)内存的大小。我的代码适用于kitkat和api版本以下,但我的应用程序在Marshmallow或棒棒糖中崩溃。这是我的代码..
public static long getAvailableInternalMemorySize()
{
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return availableBlocks * blockSize;
}
public static long getTotalInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return totalBlocks * blockSize;
}
public static long getAvailableExternalMemorySize() {
String secStore = System.getenv("SECONDARY_STORAGE");
File path = new File(secStore);
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
long total = availableBlocks * blockSize;
return total;
}
public static long getTotalExternalMemorySize() {
String secStore = System.getenv("SECONDARY_STORAGE");
File path = new File(secStore);
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getBlockCount();
long total = availableBlocks * blockSize;
return total;
}
你能告诉我在上面api版本中获取内存大小的解决方案......?
答案 0 :(得分:1)
在lolipop或marshmallo中你必须使用
的 Environment.getExternalStorageDirectory()getAbsolutePath(); 强>
获取外部存储路径而不是 System.getenv(" SECONDARY_STORAGE");
请参阅beolw代码以获取> android 5.0
中的外部和内部存储空间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() {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return formatSize(availableBlocks * blockSize);
}