내부 저장소 용량 구하기.
1. 총 용량
public static String getLocalTotalMemory() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = 0;
long totalBlocks = 0;
if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
blockSize = stat.getBlockSizeLong();
totalBlocks = stat.getBlockCountLong();
} else {
blockSize = stat.getBlockSize();
totalBlocks = stat.getBlockCount();
}
return formatMemorySize(totalBlocks * blockSize);
}
2. 사용가능한 용량
public static String getCurrentRemainLocalMemory() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = 0;
long availableBlocks = 0;
if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
blockSize = stat.getBlockSizeLong();
availableBlocks = stat.getAvailableBlocksLong();
} else {
blockSize = stat.getBlockSize();
availableBlocks = stat.getAvailableBlocks();
}
return formatMemorySize(availableBlocks * blockSize);
}
3. 용량의 단위 정하기
public static String formatMemorySize(long memory) {
String suffix = null;
double size = 0;
if(memory >= 1024){
suffix = "_KB";
size = (double) (memory / 1024);
if(size >= 1024) {
suffix = "_MB";
size = (double) (size / 1024);
if(size >= 1024) {
suffix = "_GB";
size = (double) (size / 1024);
}
}
}
size = Math.round(size * 10d)/10d;
StringBuilder resultBuffer = new StringBuilder(Double.toString(size));
if(suffix != null){
resultBuffer.append(suffix);
}
return resultBuffer.toString();
}
'안드로이드' 카테고리의 다른 글
안드로이드 문자 수신시 앱에서 문자 캐칭 (0) | 2017.03.06 |
---|---|
db -> px 단위 변경 (0) | 2017.03.06 |
안드로이드 RecyclerView 사용하기 예제 (0) | 2017.03.06 |
공유 가능 앱 리스트 가져오기 (0) | 2017.03.06 |
안드로이드 앱의 cpu 사용량 구하기 (0) | 2017.03.06 |