有没有办法在 Android 中获取 SD 卡大小?
Is there a way to get SD Card size in Android?
欢迎大家
我已经在 Whosebug 中尝试了所有与此相关的问题,并且 google 和 none 都有效。接下来我尝试了类似的方法 link,但它 returns 与内部存储相同:How to get an External storage sd card size (With Mounted SD card)?
例如,如果我有大约 12GB 的内部存储和 4GB 的 SD 卡存储,无论我使用什么方法,我总是得到与内部 [=] 完全相同的 SD space 数字28=]。
Whosebug 中发布的旧方法似乎只在 Android KitKat 之前有效,但在接下来的 android 版本中无效。
有可能解决这个问题吗?
好吧,我一直想知道这个问题,但在网上找不到答案。所以这就是我所做的。它可能不是那么干净,但每次都对我有用。
就我而言:returns 61,055 MB。我插入了 64 GB 的 SD 卡。
哦,我忘了说:我今天在 Samsung Galaxy S5 6.0 和 Sony Xperia Z5 Premium 5.1.1 上做了这个确认。但是,我也有一个每天有数百人使用的应用程序,而且我还没有遇到任何问题。
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
static String getExternalSdCardSize() {
File storage = new File("/storage");
String external_storage_path = "";
String size = "";
if (storage.exists()) {
File[] files = storage.listFiles();
for (File file : files) {
if (file.exists()) {
try {
if (Environment.isExternalStorageRemovable(file)) {
// storage is removable
external_storage_path = file.getAbsolutePath();
break;
}
} catch (Exception e) {
Log.e("TAG", e.toString());
}
}
}
}
if (!external_storage_path.isEmpty()) {
File external_storage = new File(external_storage_path);
if (external_storage.exists()) {
size = totalSize(external_storage);
}
}
return size;
}
private static String totalSize(File file) {
StatFs stat = new StatFs(file.getPath());
long blockSize, totalBlocks;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
blockSize = stat.getBlockSizeLong();
totalBlocks = stat.getBlockCountLong();
} else {
blockSize = stat.getBlockSize();
totalBlocks = stat.getBlockCount();
}
return formatSize(totalBlocks * blockSize);
}
private static String formatSize(long size) {
String suffix = null;
if (size >= 1024) {
suffix = "KB";
size /= 1024;
if (size >= 1024) {
suffix = "MB";
size /= 1024;
}
}
StringBuilder resultBuilder = new StringBuilder(Long.toString(size));
int commaOffset = resultBuilder.length() - 3;
while (commaOffset > 0) {
resultBuilder.insert(commaOffset, ',');
commaOffset -= 3;
}
if (suffix != null) resultBuilder.append(suffix);
return resultBuilder.toString();
}
我的phone有一个32GB的内置存储和15GB的SD卡。在 /mnt/sdcard
上执行 df
会得到 32GB 的结果,这不是我们想要的。进一步挖掘,我发现了这个 /storage
目录。它有 3 个文件:
shell@E5803:/storage $ ls
8E5D-12E2
emulated
self
对每个项目执行 df
得到以下结果:
shell@E5803:/storage $ df self
Filesystem Size Used Free Blksize
self 889.4M 0.0K 889.4M 4096
shell@E5803:/storage $ df emulated
Filesystem Size Used Free Blksize
emulated 22.6G 10.8G 11.8G 4096
shell@E5803:/storage $ df 8E5D-12E2/
Filesystem Size Used Free Blksize
8E5D-12E2/ 14.9G 2.3G 12.7G 32768
我认为神奇的命令是 "df /storage/" + mFilesArray[0]
,其中 mFilesArray[]
是 ls /storage
的结果。
(希望Google大佬们以后不要改SD卡挂载点,这点我很怀疑。)
希望对您有所帮助。
欢迎大家
我已经在 Whosebug 中尝试了所有与此相关的问题,并且 google 和 none 都有效。接下来我尝试了类似的方法 link,但它 returns 与内部存储相同:How to get an External storage sd card size (With Mounted SD card)?
例如,如果我有大约 12GB 的内部存储和 4GB 的 SD 卡存储,无论我使用什么方法,我总是得到与内部 [=] 完全相同的 SD space 数字28=]。
Whosebug 中发布的旧方法似乎只在 Android KitKat 之前有效,但在接下来的 android 版本中无效。
有可能解决这个问题吗?
好吧,我一直想知道这个问题,但在网上找不到答案。所以这就是我所做的。它可能不是那么干净,但每次都对我有用。
就我而言:returns 61,055 MB。我插入了 64 GB 的 SD 卡。
哦,我忘了说:我今天在 Samsung Galaxy S5 6.0 和 Sony Xperia Z5 Premium 5.1.1 上做了这个确认。但是,我也有一个每天有数百人使用的应用程序,而且我还没有遇到任何问题。
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
static String getExternalSdCardSize() {
File storage = new File("/storage");
String external_storage_path = "";
String size = "";
if (storage.exists()) {
File[] files = storage.listFiles();
for (File file : files) {
if (file.exists()) {
try {
if (Environment.isExternalStorageRemovable(file)) {
// storage is removable
external_storage_path = file.getAbsolutePath();
break;
}
} catch (Exception e) {
Log.e("TAG", e.toString());
}
}
}
}
if (!external_storage_path.isEmpty()) {
File external_storage = new File(external_storage_path);
if (external_storage.exists()) {
size = totalSize(external_storage);
}
}
return size;
}
private static String totalSize(File file) {
StatFs stat = new StatFs(file.getPath());
long blockSize, totalBlocks;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
blockSize = stat.getBlockSizeLong();
totalBlocks = stat.getBlockCountLong();
} else {
blockSize = stat.getBlockSize();
totalBlocks = stat.getBlockCount();
}
return formatSize(totalBlocks * blockSize);
}
private static String formatSize(long size) {
String suffix = null;
if (size >= 1024) {
suffix = "KB";
size /= 1024;
if (size >= 1024) {
suffix = "MB";
size /= 1024;
}
}
StringBuilder resultBuilder = new StringBuilder(Long.toString(size));
int commaOffset = resultBuilder.length() - 3;
while (commaOffset > 0) {
resultBuilder.insert(commaOffset, ',');
commaOffset -= 3;
}
if (suffix != null) resultBuilder.append(suffix);
return resultBuilder.toString();
}
我的phone有一个32GB的内置存储和15GB的SD卡。在 /mnt/sdcard
上执行 df
会得到 32GB 的结果,这不是我们想要的。进一步挖掘,我发现了这个 /storage
目录。它有 3 个文件:
shell@E5803:/storage $ ls
8E5D-12E2
emulated
self
对每个项目执行 df
得到以下结果:
shell@E5803:/storage $ df self
Filesystem Size Used Free Blksize
self 889.4M 0.0K 889.4M 4096
shell@E5803:/storage $ df emulated
Filesystem Size Used Free Blksize
emulated 22.6G 10.8G 11.8G 4096
shell@E5803:/storage $ df 8E5D-12E2/
Filesystem Size Used Free Blksize
8E5D-12E2/ 14.9G 2.3G 12.7G 32768
我认为神奇的命令是 "df /storage/" + mFilesArray[0]
,其中 mFilesArray[]
是 ls /storage
的结果。
(希望Google大佬们以后不要改SD卡挂载点,这点我很怀疑。)
希望对您有所帮助。