StorageUtils class to find storage card info
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.StringTokenizer;
import android.os.Environment;
import android.util.Log;
public class StorageUtils {
private static final String TAG = "StorageUtils";
public static class StorageInfo {
public final String path;
public final boolean readonly;
public final boolean removable;
public final int number;
StorageInfo(String path, boolean readonly, boolean removable, int number) {
this.path = path;
this.readonly = readonly;
this.removable = removable;
this.number = number;
}
public String getDisplayName() {
StringBuilder res = new StringBuilder();
if (!removable) {
res.append("Internal SD card");
} else if (number > 1) {
res.append("SD card " + number);
} else {
res.append("SD card");
}
if (readonly) {
res.append(" (Read only)");
}
return res.toString();
}
}
public static List<StorageInfo> getStorageList() {
List<StorageInfo> list = new ArrayList<StorageInfo>();
String def_path = Environment.getExternalStorageDirectory().getPath();
boolean def_path_removable = Environment.isExternalStorageRemovable();
String def_path_state = Environment.getExternalStorageState();
boolean def_path_available = def_path_state
.equals(Environment.MEDIA_MOUNTED)
|| def_path_state.equals(Environment.MEDIA_MOUNTED_READ_ONLY);
boolean def_path_readonly = Environment.getExternalStorageState()
.equals(Environment.MEDIA_MOUNTED_READ_ONLY);
HashSet<String> paths = new HashSet<String>();
int cur_removable_number = 1;
if (def_path_available) {
paths.add(def_path);
list.add(0, new StorageInfo(def_path, def_path_readonly,
def_path_removable,
def_path_removable ? cur_removable_number++ : -1));
}
BufferedReader buf_reader = null;
try {
buf_reader = new BufferedReader(new FileReader("/proc/mounts"));
String line;
Log.d(TAG, "/proc/mounts");
while ((line = buf_reader.readLine()) != null) {
Log.d(TAG, line);
if (line.contains("vfat") || line.contains("/mnt")
|| line.contains("/sdcard")) {
StringTokenizer tokens = new StringTokenizer(line, " ");
String unused = tokens.nextToken(); // device
String mount_point = tokens.nextToken(); // mount point
if (paths.contains(mount_point)) {
continue;
}
unused = tokens.nextToken(); // file system
List<String> flags = Arrays.asList(tokens.nextToken()
.split(",")); // flags
boolean readonly = flags.contains("ro");
if (line.contains("/dev/block/vold") || line.contains("/dev/fuse")) {
if (!line.contains("/mnt/secure")
&& !line.contains("/mnt/asec")
&& !line.contains("/mnt/obb")
&& !line.contains("/dev/mapper")
&& !line.contains("tmpfs")) {
paths.add(mount_point);
list.add(new StorageInfo(mount_point, readonly,
true, cur_removable_number++));
}
}
}
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (buf_reader != null) {
try {
buf_reader.close();
} catch (IOException ex) {
}
}
}
return list;
}
}
----------------------------------------
In activity add following code.
private float[] getMemInfo(String path) {
StatFs statFs = new StatFs(path);
long blockSize = statFs.getBlockSize();
float totalSize = statFs.getBlockCount() * blockSize
/ (1024f * 1024f * 1024f);
float availableSize = statFs.getAvailableBlocks() * blockSize
/ (1024f * 1024f * 1024f);
float freeSize = statFs.getFreeBlocks() * blockSize
/ (1024f * 1024f * 1024f);
return new float[] { totalSize, availableSize, freeSize };
}
Print sd-card detail using following code
// sd card data
String storage = "";
List<StorageInfo> mList = StorageUtils.getStorageList();
for (
int i =
0;
i <
mList.
size();
i++) {
try {
StorageInfo mStorageInfo = mList.get(i);
float[] mInfo = getMemInfo(mStorageInfo.path);
storage += "\n External " + mStorageInfo.number + " : total = "
+
String.
format(
"%.2f GB",
mInfo[
0])
+ " , available = "
+
String.
format(
"%.2f GB",
mInfo[
1]);
}
catch (
Exception e) {
e.printStackTrace();
}
}
---------------------------
get RAM information
/**
* RAM related functions
*/
private int getFreeRamPercentage() {
MemoryInfo mi = new MemoryInfo();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
double raminfo[] = getTotalRAM();
int percentAvail = -1;
if (
raminfo[
0] !=
0) {
percentAvail = (int) (mi.availMem * 100 / raminfo[0]);
}
return percentAvail;
}
public double[] getTotalRAM() {
RandomAccessFile reader = null;
String load = null;
DecimalFormat twoDecimalForm = new DecimalFormat("#.##");
double raminfo[] = new double[2];
String lastValue = "";
try {
reader = new RandomAccessFile("/proc/meminfo", "r");
load = reader.readLine();
Log.i("ram-info", load);
String total = parseInfoFromCommand(load);
load = reader.readLine();
Log.i("freee-ram-info", load);
String free = parseInfoFromCommand(load);
reader.
close();
raminfo[0] = Double.parseDouble(total) * 1024;
raminfo[1] = Double.parseDouble(free) * 1024;
return raminfo;
} catch (IOException ex) {
ex.printStackTrace();
} finally {
}
return raminfo;
}
private String parseInfoFromCommand(String load) {
Pattern p = Pattern.compile("(\\d+)");
Matcher m =
p.
matcher(
load);
String value = "";
while (
m.
find()) {
value =
m.
group(
1);
}
return value;
}