Example usage for android.os.storage StorageManager getStorageVolumes

List of usage examples for android.os.storage StorageManager getStorageVolumes

Introduction

In this page you can find the example usage for android.os.storage StorageManager getStorageVolumes.

Prototype

public @NonNull List<StorageVolume> getStorageVolumes() 

Source Link

Document

Return the list of shared/external storage volumes available to the current user.

Usage

From source file:net.sf.fakenames.fddemo.BaseDirLayout.java

public void init() throws IOException {
    home = getBaseDir();/*from  w  ww  .j ava  2  s  .c o  m*/

    mountInfo = MountsSingleton.get(os);

    final HashMap<File, String> pathNameMap = new HashMap<>();

    File systemRoot = Environment.getRootDirectory();
    try {
        systemRoot = systemRoot.getCanonicalFile();
    } catch (IOException ignore) {
        // ok
    }
    pathNameMap.put(systemRoot, "Android system root");

    File filesDir = getFilesDir();
    try {
        filesDir = filesDir.getCanonicalFile();
    } catch (IOException ignore) {
        // ok
    }
    pathNameMap.put(filesDir, "Internal private storage");

    File[] external = ContextCompat.getExternalFilesDirs(this, null);
    for (int i = 0; i < external.length; ++i) {
        File resolved = external[i];

        if (resolved == null)
            continue;

        try {
            resolved = resolved.getCanonicalFile();
        } catch (IOException ignore) {
            // ok
        }
        pathNameMap.put(resolved, "External storage " + i);
    }

    List<StorageVolume> volumes = new ArrayList<>();
    if (Build.VERSION.SDK_INT >= 24) {
        final StorageManager sm = (StorageManager) getSystemService(STORAGE_SERVICE);
        volumes.addAll(sm.getStorageVolumes());
    }

    final Lock lock = mountInfo.getLock();
    lock.lock();
    try {
        parseMounts(pathNameMap, volumes);
    } finally {
        lock.unlock();
    }

}