Androidはメモリ、内部記憶、外部記憶空間サイズを取得します。
4620 ワード
回転:http://blog.csdn.net/u013718120/article/details/50265643
今日は主にRAM、ROM、SDCARDの空間サイズを取得するためのツールを貼っています。直接コードを入れて、見れば分かります。はははは
今日は主にRAM、ROM、SDCARDの空間サイズを取得するためのツールを貼っています。直接コードを入れて、見れば分かります。はははは
package com.mob.getsdandphone;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
public class FileSizeUtil {
private static final int ERROR = -1;
/**
* SDCARD
*/
public static boolean externalMemoryAvailable() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
/**
*
*
* @return
*/
public static long getAvailableInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return availableBlocks * blockSize;
}
/**
*
*
* @return
*/
public static long getTotalInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return totalBlocks * blockSize;
}
/**
* SDCARD
*
* @return
*/
public static 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;
}
}
/**
* SDCARD
*
* @return
*/
public static 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;
}
}
/**
*
*
* @param context 。
* @return B。
*/
public static long getTotalMemorySize(Context context) {
String dir = "/proc/meminfo";
try {
FileReader fr = new FileReader(dir);
BufferedReader br = new BufferedReader(fr, 2048);
String memoryLine = br.readLine();
String subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:"));
br.close();
return Integer.parseInt(subMemoryLine.replaceAll("\\D+", "")) * 1024l;
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
/**
* , 。
*
* @param context 。
* @return B。
*/
public static long getAvailableMemory(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
am.getMemoryInfo(memoryInfo);
return memoryInfo.availMem;
}
private static DecimalFormat fileIntegerFormat = new DecimalFormat("#0");
private static DecimalFormat fileDecimalFormat = new DecimalFormat("#0.#");
/**
*
*
* @param size B
* @param isInteger
* @return
*/
public static String formatFileSize(long size, boolean isInteger) {
DecimalFormat df = isInteger ? fileIntegerFormat : fileDecimalFormat;
String fileSizeString = "0M";
if (size < 1024 && size > 0) {
fileSizeString = df.format((double) size) + "B";
} else if (size < 1024 * 1024) {
fileSizeString = df.format((double) size / 1024) + "K";
} else if (size < 1024 * 1024 * 1024) {
fileSizeString = df.format((double) size / (1024 * 1024)) + "M";
} else {
fileSizeString = df.format((double) size / (1024 * 1024 * 1024)) + "G";
}
return fileSizeString;
}
}