Skip to content

Instantly share code, notes, and snippets.

@Azazle
Last active June 8, 2018 18:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Azazle/865fe798a578bee0e0ec to your computer and use it in GitHub Desktop.
Save Azazle/865fe798a578bee0e0ec to your computer and use it in GitHub Desktop.
get Android device's all external storage
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.os.StatFs;
import android.os.storage.StorageManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class StorageUtil {
private final static int MAX_EXTERNAL_STORAGE_NUM = 4;
private final static String VOLUME_MOUNTED_STATE = "mounted";
public static ArrayList<String> getAllExternalStorages(Context context) {
ArrayList<String> pathList = new ArrayList<String>(MAX_EXTERNAL_STORAGE_NUM);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
pathList = getExternalPaths(context);
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
pathList = getVolumePaths(context);
} else {
pathList = filterExternalPaths();
}
return pathList;
}
@SuppressWarnings("deprecation")
public static long getAvailableSpace(String path) {
StatFs statFs = new StatFs(path);
long blockSize = statFs.getBlockSize();
long availableBlocks = statFs.getAvailableBlocks();
return blockSize * availableBlocks;
}
@SuppressLint("NewApi")
private static ArrayList<String> getExternalPaths(Context context) {
File[] externalFiles = context.getExternalFilesDirs(null);
int len = externalFiles.length + 1;
ArrayList<String> externalPathList = new ArrayList<String>(len);
for (File externalFile : externalFiles) {
if (externalFile != null && externalFile.exist()) {
externalPathList.add(externalFile.getPath());
}
}
return externalPathList;
}
private static ArrayList<String> getVolumePaths(Context context) {
StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
ArrayList<String> pathList = new ArrayList<String>(MAX_EXTERNAL_STORAGE_NUM);
try {
Method getListMethod = StorageManager.class.getMethod("getVolumePaths");
String[] volumeList = (String[]) getListMethod.invoke(sm);
if (volumeList != null) {
for (int i = 0; i < volumeList.length; i++) {
Method volumeMethod = StorageManager.class.getMethod("getVolumeState", String.class);
String path = volumeList[i];
String volumeState = (String) volumeMethod.invoke(sm, path);
if (VOLUME_MOUNTED_STATE.equals(volumeState) && !pathList.contains(path)) {
pathList.add(path);
}
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return pathList;
}
private static ArrayList<String> filterExternalPaths() {
ArrayList<String> procMountList = getProcMounts();
List<String> mountPointList = getMountPoints();
Iterator<String> iterator = procMountList.iterator();
while (iterator.hasNext()) {
String mountPoint = iterator.next();
if (!mountPointList.contains(mountPoint)) {
iterator.remove();
}
}
return procMountList;
}
private static ArrayList<String> getMountPoints() {
ArrayList<String> mountPointList = new ArrayList<String>();
File voldFstab = new File("/system/etc/vold.fstab");
Scanner scanner = null;
try {
scanner = new Scanner(new FileInputStream(voldFstab));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("dev_mount") || line.startsWith("fuse_mount")) {
String[] lineSplits = line.split("\\s+");
String mountPoint = lineSplits[2];
if (mountPoint.contains(":")) {
mountPoint = mountPoint.substring(0, mountPoint.indexOf(":"));
}
mountPointList.add(mountPoint);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (scanner != null) scanner.close();
}
return mountPointList;
}
private static ArrayList<String> getProcMounts() {
ArrayList<String> procMountList = new ArrayList<String>();
File procMounts = new File("/proc/mounts");
Scanner scanner = null;
try {
scanner = new Scanner(new FileInputStream(procMounts));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("/dev/block") && !line.contains("asec")) {
String[] lineSplits = line.split("\\s+");
String procMount = lineSplits[1];
if (!procMountList.contains(procMount)) {
procMountList.add(procMount);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (scanner != null) scanner.close();
}
return procMountList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment