简体   繁体   中英

why am I getting incorrect phone and SDcard memory information?

I want to see the right information from the phone settings such as in SDCART and phone memory. but see this code card 1800 MB total memory. my card is 16GB. could not figure out. I've tried a lot of code, but did not. How do I solve this problem.

public static boolean externalMemoryAvailable() { return android.os.Environment.getExternalStorageState().equals( 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 = new 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(); } 

From Environment#getExternalStorageDirectory()

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

There's two internal storages. One for apps and system and one for data. The one for data acts like a first SD card. (This is very dumbed down version, please forgive me.) It's also the one you get by calling the getExternalStorageDirectory() method.

From the same source

In devices with multiple "external" storage directories, this directory represents the "primary" external storage that the user will interact with. Access to secondary storage is available through

I'm not kidding you, the sentence ends there. I found another answer which is supposed to help you list available "SD cards" or external storages. Take it apart and use what you need. I think it will look something like this:

String externals = System.getenv("SECONDARY_STORAGE");
String[] externalsArray = externals.split(":");
String secondaryExternal = externalsArray[0];
File path = new File(secondaryExternal);

Please note that I have not tested the above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM