좋아, 나는 항상 이것을 궁금해하고 온라인에서 답을 찾을 수 없었다. 그래서 여기에 내가하는 일이 있습니다. 그것은 너무 깨끗하지 않을 수도 있지만 그것은 매번 저를 위해 일합니다.
내 경우 : 61,055MB를 반환합니다. 64GB sd 카드를 삽입했습니다.
아, 그리고 내가 언급하는 것을 잊었다 : 나는 확인 5.1.1 오늘 삼성 갤럭시 S5 6.0 및 소니 엑스 페리아 Z5 프리미엄에 이런 짓을. 그러나, 나는 또한 매일 수백 명의 사람들이 사용하는 앱을 가지고 있으며 아직 어떤 문제도 경험하지 못했습니다.
@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();
}
잘 사용하셨습니까? 게시 한 코드는 SD 카드와 아무 관련이 없습니다. – greenapps
@ greenapps 20 개의 솔루션을 스택에 넣은 것처럼 tryed했습니다. 그들 모두를 기억할 수는 없다. 단순히 그들 모두를 tryed. 그들 중 누구도 4.4 kitkat 이후로 일하지 않습니다. 작동하는 솔루션을 알고 있습니까? – NullPointerException
나는 당신이 시도한 것을 모른다. 그리고 당신은 당신이 SD 카드로 가져간 경로를 말하지 않았습니다. 네가 그 길을 말했을 뿐이야. – greenapps