Example usage for android.os.storage StorageManager getClass

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

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:Main.java

public static boolean hasExtSdcard(Context context) {
    String[] paths;//w w w  .j  ava  2s  .c om
    StorageManager service = (StorageManager) context.getSystemService(Activity.STORAGE_SERVICE);
    try {
        Method mMethod = service.getClass().getMethod("getVolumePaths");
        paths = (String[]) mMethod.invoke(service);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static String getAvailableStoragePath(Context context) {
    if (BASE_DIR == null) {
        try {//from   w w  w . j a v  a2  s.c om
            BASE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath();
            StorageManager sStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);

            Class<?> smClazz = sStorageManager.getClass();
            Method listMethod = smClazz.getDeclaredMethod("getVolumeList");
            Object vlObject = listMethod.invoke(sStorageManager);

            if (vlObject.getClass().isArray()) {
                String state = null;
                String path = null;
                // Class svClazz =
                // Class.forName("android.os.storage.StorageVolume");
                Object svObject = Array.get(vlObject, 1);
                if (svObject != null) {
                    Method pathMethod = svObject.getClass().getMethod("getPath");
                    path = (String) pathMethod.invoke(svObject);

                    Method stateMethod = smClazz.getMethod("getVolumeState", new Class[] { String.class });
                    state = (String) stateMethod.invoke(sStorageManager, path);
                }

                if (path != null && state != null && state.equals(Environment.MEDIA_MOUNTED)) {
                    BASE_DIR = path;
                } else {
                    BASE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath();
                }
            }
        } catch (Exception e) {
            BASE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath();
        }

        return BASE_DIR;
    } else {
        return BASE_DIR;
    }

}

From source file:Main.java

private static String[] getExternalDirs(Context context) {
    Context mContext = context.getApplicationContext();
    StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
    try {//from   w ww .ja  v a2  s .c o m
        Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
        Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
        Method getPath = storageVolumeClazz.getMethod("getPath");
        Object result = getVolumeList.invoke(mStorageManager);
        final int length = Array.getLength(result);
        final String[] paths = new String[length];
        for (int i = 0; i < length; i++) {
            Object storageVolumeElement = Array.get(result, i);
            paths[i] = (String) getPath.invoke(storageVolumeElement);
        }
        return paths;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Get the path of a certain volume./*  ww w. j  a  v a 2s  .  co  m*/
 *
 * @param volumeId The volume id.
 * @return The path.
 */
private static String getVolumePath(final String volumeId) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return null;
    }

    try {
        StorageManager mStorageManager = (StorageManager) applicationContext
                .getSystemService(Context.STORAGE_SERVICE);

        Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");

        Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
        Method getUuid = storageVolumeClazz.getMethod("getUuid");
        Method getPath = storageVolumeClazz.getMethod("getPath");
        Method isPrimary = storageVolumeClazz.getMethod("isPrimary");
        Object result = getVolumeList.invoke(mStorageManager);

        final int length = Array.getLength(result);
        for (int i = 0; i < length; i++) {
            Object storageVolumeElement = Array.get(result, i);
            String uuid = (String) getUuid.invoke(storageVolumeElement);
            Boolean primary = (Boolean) isPrimary.invoke(storageVolumeElement);

            // primary volume?
            if (primary && PRIMARY_VOLUME_NAME.equals(volumeId)) {
                return (String) getPath.invoke(storageVolumeElement);
            }

            // other volumes?
            if (uuid != null) {
                if (uuid.equals(volumeId)) {
                    return (String) getPath.invoke(storageVolumeElement);
                }
            }
        }

        // not found.
        return null;
    } catch (Exception ex) {
        return null;
    }
}

From source file:com.frostwire.android.gui.StoragePicker.java

private static String getVolumePath(StorageManager mStorageManager, final String volumeId) {
    try {/*w  ww. j  a v  a  2  s  . c  o  m*/
        Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");

        Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
        Method getUuid = storageVolumeClazz.getMethod("getUuid");
        Method getPath = storageVolumeClazz.getMethod("getPath");
        Method isPrimary = storageVolumeClazz.getMethod("isPrimary");
        Object result = getVolumeList.invoke(mStorageManager);

        final int length = Array.getLength(result);
        for (int i = 0; i < length; i++) {
            Object storageVolumeElement = Array.get(result, i);
            String uuid = (String) getUuid.invoke(storageVolumeElement);
            Boolean primary = (Boolean) isPrimary.invoke(storageVolumeElement);

            // primary volume?
            if (primary && PRIMARY_VOLUME_NAME.equals(volumeId)) {
                return (String) getPath.invoke(storageVolumeElement);
            }

            // other volumes?
            if (uuid != null) {
                if (uuid.equals(volumeId)) {
                    return (String) getPath.invoke(storageVolumeElement);
                }
            }
        }

        // not found.
        return null;
    } catch (Exception ex) {
        return null;
    }
}

From source file:com.frostwire.android.LollipopFileSystem.java

private static String getVolumePath(StorageManager mStorageManager, final String volumeId) {
    try {//from w w w  . jav  a 2 s. c  o  m
        Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");

        Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
        Method getUuid = storageVolumeClazz.getMethod("getUuid");
        Method getPath = storageVolumeClazz.getMethod("getPath");
        Method isPrimary = storageVolumeClazz.getMethod("isPrimary");
        Object result = getVolumeList.invoke(mStorageManager);

        final int length = Array.getLength(result);
        for (int i = 0; i < length; i++) {
            Object storageVolumeElement = Array.get(result, i);
            String uuid = (String) getUuid.invoke(storageVolumeElement);
            Boolean primary = (Boolean) isPrimary.invoke(storageVolumeElement);

            // primary volume?
            if (primary && "primary".equals(volumeId)) {
                return (String) getPath.invoke(storageVolumeElement);
            }

            // other volumes?
            if (uuid != null) {
                if (uuid.equals(volumeId)) {
                    return (String) getPath.invoke(storageVolumeElement);
                }
            }
        }

        // not found.
        return null;
    } catch (Exception ex) {
        return null;
    }
}

From source file:com.frostwire.android.LollipopFileSystem.java

private static String getVolumeId(Context context, final String volumePath) {
    try {//from  w  ww . ja  v  a2 s.  co  m
        StorageManager mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);

        Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");

        Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
        Method getUuid = storageVolumeClazz.getMethod("getUuid");
        Method getPath = storageVolumeClazz.getMethod("getPath");
        Object result = getVolumeList.invoke(mStorageManager);

        final int length = Array.getLength(result);
        for (int i = 0; i < length; i++) {
            Object storageVolumeElement = Array.get(result, i);
            String path = (String) getPath.invoke(storageVolumeElement);

            if (path != null) {
                if (path.equals(volumePath)) {
                    return (String) getUuid.invoke(storageVolumeElement);
                }
            }
        }

        // not found.
        return null;
    } catch (Exception ex) {
        return null;
    }
}