How to Get the List of Mounted External Storage of Android Device

How to list additional external storage folders (mount points)?

You can read /proc/mounts to find out what is currently mounted.

Or you can read /etc/vold.conf or /etc/vold.fstab (it depends on version what config file is present). This files contains config for removable storages. At tablet default storage is not usually present in vold.

Get the list of paths of all the Storage Devices connected to an Android device

You can create one class EnvironmentSDCardCheck

package com.example.storagecheck;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.os.EnvironmentCompat;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class EnvironmentSDCardCheck {
private static final String TAG = "EnvironmentSDCardCheck";

public final static String TYPE_PRIMARY = "primär";
public final static String TYPE_INTERNAL = "intern";
public final static String TYPE_SD = "MicroSD";
public final static String TYPE_USB = "USB";
public final static String TYPE_UNKNOWN = "unbekannt";

public final static String WRITE_NONE = "none";
public final static String WRITE_READONLY = "readonly";
public final static String WRITE_APPONLY = "apponly";
public final static String WRITE_FULL = "readwrite";

private static Device[] devices, externalstorage, storage;
private static BroadcastReceiver receiver;
private static boolean useReceiver = true;
private static String userDir;

public static Device[] getDevices(Context context) {
if (devices == null) initDevices(context);
return devices;
}

public static Device[] getExternalStorage(Context context) {
if (devices == null) initDevices(context);
return externalstorage;
}

public static Device[] getStorage(Context context) {
if (devices == null) initDevices(context);
return storage;
}

public static IntentFilter getRescanIntentFilter() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
filter.addAction(Intent.ACTION_MEDIA_SHARED);
filter.addDataScheme("file");
return filter;
}

public static void setUseReceiver(Context context, boolean use) {
if (use && receiver == null) {
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Storage " + intent.getAction() + "-" + intent.getData());
initDevices(context);
}
};
context.registerReceiver(receiver, getRescanIntentFilter());
} else if (!use && receiver != null) {
context.unregisterReceiver(receiver);
receiver = null;
}
useReceiver = use;
}

public static void initDevices(Context context) {
if (userDir == null) userDir = "/Android/data/" + context.getPackageName();
setUseReceiver(context, useReceiver);
StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
Class c = sm.getClass();
Object[] vols;
try {
Method m = c.getMethod("getVolumeList", null);
vols = (Object[]) m.invoke(sm, null); // android.os.Storage.StorageVolume
Device[] temp = new Device[vols.length];
for (int i = 0; i < vols.length; i++) temp[i] = new Device(vols[i]);
Device primary = null;
for (Device d : temp) if (d.mPrimary) primary = d;
if (primary == null) for (Device d : temp)
if (!d.mRemovable) {
d.mPrimary = true;
primary = d;
break;
}
if (primary == null) {
primary = temp[0];
primary.mPrimary = true;
}

File[] files = ContextCompat.getExternalFilesDirs(context, null);
File[] caches = ContextCompat.getExternalCacheDirs(context);
for (Device d : temp) {
if (files != null) for (File f : files)
if (f != null && f.getAbsolutePath().startsWith(d.getAbsolutePath()))
d.mFiles = f;
if (caches != null) for (File f : caches)
if (f != null && f.getAbsolutePath().startsWith(d.getAbsolutePath()))
d.mCache = f;
}

ArrayList<Device> tempDev = new ArrayList<Device>(10);
ArrayList<Device> tempStor = new ArrayList<Device>(10);
ArrayList<Device> tempExt = new ArrayList<Device>(10);
for (Device d : temp) {
tempDev.add(d);
if (d.isAvailable()) {
tempExt.add(d);
tempStor.add(d);
}
}

Device internal = new Device(context);
tempStor.add(0, internal); // bei Storage-Alternativen immer
if (!primary.mEmulated) tempDev.add(0, internal); // bei Devices nur wenn zusätzlich

devices = tempDev.toArray(new Device[tempDev.size()]);
storage = tempStor.toArray(new Device[tempStor.size()]);
externalstorage = tempExt.toArray(new Device[tempExt.size()]);
} catch (Exception e) {
// Fallback auf normale Android-Funktionen
}

}

public static class Device extends File {
String mUserLabel, mUuid, mState, mWriteState, mType;
boolean mPrimary, mRemovable, mEmulated, mAllowMassStorage;
long mMaxFileSize;
File mFiles, mCache;

Device(Context context) {
super(Environment.getDataDirectory().getAbsolutePath());
mState = Environment.MEDIA_MOUNTED;
mFiles = context.getFilesDir();
mCache = context.getCacheDir();
mType = TYPE_INTERNAL;
mWriteState = WRITE_APPONLY;
}

@SuppressWarnings("NullArgumentToVariableArgMethod")
Device(Object storage) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
super((String) storage.getClass().getMethod("getPath", null).invoke(storage, null));
for (Method m : storage.getClass().getMethods()) {
if (m.getName().equals("getUserLabel") && m.getParameterTypes().length == 0 && m.getReturnType() == String.class)
mUserLabel = (String) m.invoke(storage, null); // ab Android 4.4
if (m.getName().equals("getUuid") && m.getParameterTypes().length == 0 && m.getReturnType() == String.class)
mUuid = (String) m.invoke(storage, null); // ab Android 4.4
if (m.getName().equals("getState") && m.getParameterTypes().length == 0 && m.getReturnType() == String.class)
mState = (String) m.invoke(storage, null); // ab Android 4.4
if (m.getName().equals("isRemovable") && m.getParameterTypes().length == 0 && m.getReturnType() == boolean.class)
mRemovable = (Boolean) m.invoke(storage, null); // ab Android 4.0
if (m.getName().equals("isPrimary") && m.getParameterTypes().length == 0 && m.getReturnType() == boolean.class)
mPrimary = (Boolean) m.invoke(storage, null); // ab Android 4.2
if (m.getName().equals("isEmulated") && m.getParameterTypes().length == 0 && m.getReturnType() == boolean.class)
mEmulated = (Boolean) m.invoke(storage, null); // ab Android 4.0
if (m.getName().equals("allowMassStorage") && m.getParameterTypes().length == 0 && m.getReturnType() == boolean.class)
mAllowMassStorage = (Boolean) m.invoke(storage, null); // ab Android 4.0
if (m.getName().equals("getMaxFileSize") && m.getParameterTypes().length == 0 && m.getReturnType() == long.class)
mMaxFileSize = (Long) m.invoke(storage, null); // ab Android 4.0
// getDescription (ab 4.1 mit context) liefert keine sinnvollen Werte
// getPathFile (ab 4.2) liefert keine sinnvollen Werte
// getMtpReserveSpace (ab 4.0) für diese Zwecke unwichtig
// getStorageId (ab 4.0) für diese Zwecke unwichtig
}
if (mState == null) mState = getState();

if (mPrimary)
mType = TYPE_PRIMARY;
else {
String n = getAbsolutePath().toLowerCase();
if (n.indexOf("sd") > 0)
mType = TYPE_SD;
else if (n.indexOf("usb") > 0)
mType = TYPE_USB;
else
mType = TYPE_UNKNOWN + " " + getAbsolutePath();
}
}

public String getType() {
return mType;
}

public String getAccess() {
if (mWriteState == null) {
try {
mWriteState = WRITE_NONE;
File[] root = listFiles();
if (root == null || root.length == 0)
throw new IOException("root empty/unreadable");
mWriteState = WRITE_READONLY;
File t = File.createTempFile("jow", null, getFilesDir());
//noinspection ResultOfMethodCallIgnored
t.delete();
mWriteState = WRITE_APPONLY;
t = File.createTempFile("jow", null, this);
//noinspection ResultOfMethodCallIgnored
t.delete();
mWriteState = WRITE_FULL;
} catch (IOException ignore) {
Log.v(TAG, "test " + getAbsolutePath() + " ->" + mWriteState + "<- " + ignore.getMessage());
}
}
return mWriteState;
}

public boolean isAvailable() {
String s = getState();
return (
Environment.MEDIA_MOUNTED.equals(s) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(s)
);
// MEDIA_SHARED: als USB freigegeben; bitte Handy auf MTP umstellen
}

public String getState() {
if (mRemovable || mState == null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
// Android 5.0? Da gibts was neues
mState = Environment.getExternalStorageState(this);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
// Android 4.4? Dann dort nachfragen
mState = Environment.getStorageState(this);
else if (canRead() && getTotalSpace() > 0)
// lesbar und Größe vorhanden => gibt es
mState = Environment.MEDIA_MOUNTED;
else if (mState == null || Environment.MEDIA_MOUNTED.equals(mState))
// nicht lesbar, keine Größe aber noch MOUNTED || oder ungesetzt => UNKNOWN
mState = EnvironmentCompat.MEDIA_UNKNOWN;
}
return mState;
}

public File getFilesDir() {
if (mFiles == null) {
mFiles = new File(this, userDir + "/files");
if (!mFiles.isDirectory())
//noinspection ResultOfMethodCallIgnored
mFiles.mkdirs();
}
return mFiles;
}

public File getCacheDir() {
if (mCache == null) {
mCache = new File(this, userDir + "/cache");
if (!mCache.isDirectory())
//noinspection ResultOfMethodCallIgnored
mCache.mkdirs();
}
return mCache;
}

public boolean isPrimary() {
return mPrimary;
}

public boolean isRemovable() {
return mRemovable;
}
public boolean isEmulated() {
return mEmulated;
}

public boolean isAllowMassStorage() {
return mAllowMassStorage;
}

public long getMaxFileSize() {
return mMaxFileSize;
}

public String getUserLabel() {
return mUserLabel;
}

public String getUuid() {
return mUuid;
}
}
}

and then you can use it to check sd card or Usb or unknown is presently connected or not with device

This way you can get the connected sd card, usb, etc.

private boolean checkSdCardPermission() {
boolean flag = false;
try {
EnvironmentSDCard.Device[] devices = EnvironmentSDCard.getExternalStorage(MainActivity.this);
for (EnvironmentSDCard.Device d : devices) {
if (d.getType().equals(EnvironmentSDCard.TYPE_SD) || d.getType().contains(EnvironmentSDCard.TYPE_UNKNOWN) || d.getType().contains(EnvironmentSDCard.TYPE_USB)) {
flag = d.isAvailable();
if (flag)
break;
}
}
} catch (Exception e) {
}
return flag;
}

android get all external storage path for all devices

I'm using this method:

    public static final String SD_CARD = "sdCard";
public static final String EXTERNAL_SD_CARD = "externalSdCard";
private static final String ENV_SECONDARY_STORAGE = "SECONDARY_STORAGE";

public static Map<String, File> getAllStorageLocations() {
Map<String, File> storageLocations = new HashMap<>(10);
File sdCard = Environment.getExternalStorageDirectory();
storageLocations.put(SD_CARD, sdCard);
final String rawSecondaryStorage = System.getenv(ENV_SECONDARY_STORAGE);
if (!TextUtils.isEmpty(rawSecondaryStorage)) {
String[] externalCards = rawSecondaryStorage.split(":");
for (int i = 0; i < externalCards.length; i++) {
String path = externalCards[i];
storageLocations.put(EXTERNAL_SD_CARD + String.format(i == 0 ? "" : "_%d", i), new File(path));
}
}
return storageLocations;
}

Get all storages and devices with their names android

I found part of the solution, however I won't get any farther because it's getting complicated and this is not the main feature of my app anyway.

public static class Storage extends File {

public static final int INTERNAL_STORAGE = 1;
public static final int SD_CARD = 2;
public static final int USB_DRIVE = 3;

public String name;
public int type;

public Storage(String path, String name, int type) {
super(path);
this.name = name;
this.type = type;
}
}

public static ArrayList<Storage> getStorages(Context context) {
ArrayList<Storage> storages = new ArrayList<>();

// Internal storage
storages.add(new Storage(Environment.getExternalStorageDirectory().getPath(),
"Internal Storage", Storage.INTERNAL_STORAGE));

// SD Cards
ArrayList<File> extStorages = new ArrayList<>();
extStorages.addAll(Arrays.asList(context.getExternalFilesDirs(null)));
extStorages.remove(0); // Remove internal storage
String secondaryStoragePath = System.getenv("SECONDARY_STORAGE");
for (int i = 0; i < extStorages.size(); i++) {
String path = extStorages.get(i).getPath().split("/Android")[0];
if (Environment.isExternalStorageRemovable(extStorages.get(i)) || secondaryStoragePath != null && secondaryStoragePath.contains(path)) {
String name = "SD Card" + (i == 0 ? "" : " " + String.valueOf(i+1));
storages.add(new Storage(path, name, Storage.SD_CARD));
}
}

// USB Drives
ArrayList<String> drives = new ArrayList<>();
String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*";
String s = "";
try {
final Process process = new ProcessBuilder().command("mount")
.redirectErrorStream(true).start();
process.waitFor();
final InputStream is = process.getInputStream();
final byte[] buffer = new byte[1024];
while (is.read(buffer) != -1) {
s += new String(buffer);
}
is.close();
} catch (final Exception e) {
e.printStackTrace();
}
final String[] lines = s.split("\n");
for (String line : lines) {
if (!line.toLowerCase(Locale.US).contains("asec") && line.matches(reg)) {
String[] parts = line.split(" ");
for (String path : parts) {
if (path.startsWith(File.separator) && !path.toLowerCase(Locale.US).contains("vold")) {
drives.add(path);
}
}
}
}

// Remove SD Cards from found drives (already found)
ArrayList<String> ids = new ArrayList<>();
for (Storage st : storages) {
String[] parts = st.getPath().split(File.separator);
ids.add(parts[parts.length-1]);
}
for (int i = drives.size() - 1; i >= 0; i--) {
String[] parts = drives.get(i).split(File.separator);
String id = parts[parts.length-1];
if (ids.contains(id)) drives.remove(i);
}

// Get USB Drive name
UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
Collection<UsbDevice> dList = usbManager.getDeviceList().values();
ArrayList<UsbDevice> deviceList = new ArrayList<>();
deviceList.addAll(dList);
for (int i = 0; i < deviceList.size(); i++) {
storages.add(new Storage(drives.get(i), deviceList.get(i).getProductName(), Storage.USB_DRIVE));
}

return storages;
}
  • Find internal storage
  • Find all SD Cards
  • Find all external drives, then remove SD Cards from them, because they were already found. The point of this is to separate SD Cards from USB devices.

I didn't test this with any other device than mine, it won't likely work for every device. Also it is possible that keyboards or mouses counts as connected devices and that will screw up the whole thing.

How to list all storage (internal and external) and absolute path in Android?

You can read the kernel exported file /proc/mounts to get the entire list of mounted file system on your device. The seconds column from /proc/mounts is the mounted point.

BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/mounts")));
List<String> mountPoints = new ArrayList<>();
String l;
while ((l = reader.readLine()) != null) {
String p = l.split("\\s+")[1];
if (p != null && p.startsWith(File.separator)) {
mountPoints.add(p);
}
}

Log.d(TAG, "all mount absolute points " + Arrays.toString(mountPoints.toArray()));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Get a list of external storage in Android 4.3

I ended up scanning /proc/mounts output for currently mounted storage. Code similar to below.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
File voldFile = new File("/proc/mounts");

fr = new FileReader(voldFile);
br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
Log.d(TAG, line);
if (line.startsWith("/")) {
String[] tokens = line.split("\\s+");
if ("vfat".equals(tokens[2])) {
File mountPoint = new File(tokens[1]);
if (!tokens[1].equals(defaultMount))
if (mountPoint.isDirectory() && mountPoint.canRead())
list.add(tokens[1]);
}
}
line = br.readLine();
}
}


Related Topics



Leave a reply



Submit