获取内部和外部存储信息- android
get internal and external storage information- android
我正在开发 android 文件管理器应用程序。
所以在主要 activity 我想显示所有可用的存储类型,如内部存储和外部 SD 卡。
所以我使用了这段代码,
public static boolean externalMemoryAvailable() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
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() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return availableBlocks * blockSize;
} else {
return 0;
}
}
public static long getTotalExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return totalBlocks * blockSize;
} else {
return 0;
}
}
但问题是,它为内部和外部存储提供了相同的内存输出。
实际上它给出了内部存储的正确答案。但是外置sd卡是错误的。
我想我获取ext sd卡的路径是错误的。有帮助吗?请
不要忘记设置外部存储的权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
另见
是的,不同品牌的androidSD卡位置路径不同,无法保证。
我有一个解决方案,但这适用于 minSdkVersion 19。
static File dirs[];
dirs = ContextCompat.getExternalFilesDirs(context, null);
//dirs[0] refers to internal memory and dirs[1] gives you external. Call the following methods to get total and available memory details.
public static String getTotalExternalMemorySize(File dirs[]) {
if (dirs.length > 1) {
StatFs stat = new StatFs(dirs[1].getPath());
long blockSize = stat.getBlockSizeLong();
long totalBlocks = stat.getBlockCountLong();
return readableFileSize(totalBlocks * blockSize);
} else {
return "NA";
}
public static String getAvailableExternalMemorySize(File[] dirs) {
if (dirs.length > 1) {
StatFs stat = new StatFs(dirs[1].getPath());
long blockSize = stat.getBlockSizeLong();
long availableBlocks = stat.getAvailableBlocksLong();
return readableFileSize(availableBlocks * blockSize);
} else {
return "NA";
}
}
public static String readableFileSize(long size) {
if(size <= 0) return "0";
final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" };
int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
return new DecimalFormat("#,##0.##").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
我正在开发 android 文件管理器应用程序。 所以在主要 activity 我想显示所有可用的存储类型,如内部存储和外部 SD 卡。
所以我使用了这段代码,
public static boolean externalMemoryAvailable() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
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() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return availableBlocks * blockSize;
} else {
return 0;
}
}
public static long getTotalExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return totalBlocks * blockSize;
} else {
return 0;
}
}
但问题是,它为内部和外部存储提供了相同的内存输出。 实际上它给出了内部存储的正确答案。但是外置sd卡是错误的。
我想我获取ext sd卡的路径是错误的。有帮助吗?请
不要忘记设置外部存储的权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
另见
是的,不同品牌的androidSD卡位置路径不同,无法保证。 我有一个解决方案,但这适用于 minSdkVersion 19。
static File dirs[];
dirs = ContextCompat.getExternalFilesDirs(context, null);
//dirs[0] refers to internal memory and dirs[1] gives you external. Call the following methods to get total and available memory details.
public static String getTotalExternalMemorySize(File dirs[]) {
if (dirs.length > 1) {
StatFs stat = new StatFs(dirs[1].getPath());
long blockSize = stat.getBlockSizeLong();
long totalBlocks = stat.getBlockCountLong();
return readableFileSize(totalBlocks * blockSize);
} else {
return "NA";
}
public static String getAvailableExternalMemorySize(File[] dirs) {
if (dirs.length > 1) {
StatFs stat = new StatFs(dirs[1].getPath());
long blockSize = stat.getBlockSizeLong();
long availableBlocks = stat.getAvailableBlocksLong();
return readableFileSize(availableBlocks * blockSize);
} else {
return "NA";
}
}
public static String readableFileSize(long size) {
if(size <= 0) return "0";
final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" };
int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
return new DecimalFormat("#,##0.##").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}