我想從手機設置中看到正確的信息,例如在SDCART和手機存儲器中。但看到這個代碼卡1800 MB的總內存。我的卡是16GB。無法弄清楚。我嘗試了很多代碼,但沒有。我如何解決這個問題。 。爲什麼我獲取不正確的手機和SD卡內存信息?
公共靜態布爾externalMemoryAvailable(){ 回報android.os.Environment.getExternalStorageState()等於( android.os.Environment.MEDIA_MOUNTED); }
public static String getAvailableInternalMemorySize() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return formatSize(availableBlocks * blockSize); } 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() { if (externalMemoryAvailable()) { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getAbsolutePath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return formatSize(availableBlocks * blockSize); } else { return ERROR; } } public static String getTotalExternalMemorySize() { if (externalMemoryAvailable()) { /* File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getAbsolutePath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); return formatSize(totalBlocks * blockSize);
*/ 的statfs STAT =新的statfs(Environment.getExternalStorageDirectory()的getPath()); long bytesAvailable =(long)stat.getBlockSize()*(long)stat.getAvailableBlocks(); long megAvailable = bytesAvailable /(1024 * 1024); return formatSize((long)stat.getBlockSize()*(long)stat.getAvailableBlocks());
} else { return ERROR; } } public static String formatSize(long size) { String suffix = null; if (size >= 1024) { suffix = " KB"; size /= 1024; if (size >= 1024) { suffix = " MB"; size /= 1024; } } StringBuilder resultBuffer = new StringBuilder(Long.toString(size)); int commaOffset = resultBuffer.length() - 3; while (commaOffset > 0) { resultBuffer.insert(commaOffset, ','); commaOffset -= 3; } if (suffix != null) resultBuffer.append(suffix); return resultBuffer.toString(); }