Memory Size Class for viewing available storage 안드로이드 SD 외장 메모리 용량 sdcard
Android :
2011. 9. 30. 11:26
반응형
출처 : http://www.androidsnippets.com/memory-size-class-for-viewing-available-storage
Class used to view available and total storage for internal and external memory. Also does pretty formatting.
Class used to view available and total storage for internal and external memory. Also does pretty formatting.
- import java.io.File;
- import android.os.Environment;
- import android.os.StatFs;
- public class MemoryStatus {
- static final int ERROR = -1;
- static public boolean externalMemoryAvailable() {
- return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
- }
- static public long getAvailableInternalMemorySize() {
- File path = Environment.getDataDirectory();
- StatFs stat = new StatFs(path.getPath());
- long blockSize = stat.getBlockSize();
- long availableBlocks = stat.getAvailableBlocks();
- return availableBlocks * blockSize;
- }
- static public long getTotalInternalMemorySize() {
- File path = Environment.getDataDirectory();
- StatFs stat = new StatFs(path.getPath());
- long blockSize = stat.getBlockSize();
- long totalBlocks = stat.getBlockCount();
- return totalBlocks * blockSize;
- }
- static public 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 ERROR;
- }
- }
- static public 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 ERROR;
- }
- }
- static public String formatSize(long size) {
- String suffix = null;
- if (size >= 1024) {
- suffix = "KiB";
- size /= 1024;
- if (size >= 1024) {
- suffix = "MiB";
- 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();
- }
- }
반응형
'Android' 카테고리의 다른 글
안드로이드 GPS 속도 구하기. android gps speed (0) | 2011.10.08 |
---|---|
Android.mk jar 파일 LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES 로 추가하기 (0) | 2011.09.26 |
부팅시에 Service 자동으로 띄우고 Service랑 Activity간에 AIDL로 통신하기 (Service랑 Activity는 완전 별개의 어플) (0) | 2011.09.20 |