Java Code Examples for android.os.Environment#getStorageState()

The following examples show how to use android.os.Environment#getStorageState() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: atlas   File: KernalVersionManager.java    License: Apache License 2.0 6 votes vote down vote up
private String getStorageState(File path) {
    if(path.getAbsolutePath().startsWith(KernalConstants.baseContext.getFilesDir().getAbsolutePath())){
        return MEDIA_MOUNTED;
    }
    final int version = Build.VERSION.SDK_INT;
    if (version >= 19) {
        return Environment.getStorageState(path);
    }

    try {
        final String canonicalPath = path.getCanonicalPath();
        final String canonicalExternal = Environment.getExternalStorageDirectory()
                .getCanonicalPath();

        if (canonicalPath.startsWith(canonicalExternal)) {
            return Environment.getExternalStorageState();
        }
    } catch (IOException e) {
    }

    return MEDIA_UNKNOWN;
}
 
Example 2
Source Project: atlas   File: Framework.java    License: Apache License 2.0 6 votes vote down vote up
public static String getStorageState(File path) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 19) {
        return Environment.getStorageState(path);
    }

    try {
        final String canonicalPath = path.getCanonicalPath();
        final String canonicalExternal = Environment.getExternalStorageDirectory()
                .getCanonicalPath();

        if (canonicalPath.startsWith(canonicalExternal)) {
            return Environment.getExternalStorageState();
        }
    } catch (IOException e) {
    }

    return MEDIA_UNKNOWN;
}
 
Example 3
Source Project: osmdroid   File: StorageUtils.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
private static List<StorageInfo> getStorageListApi19(Context context) {
    ArrayList<StorageInfo> storageInfos = new ArrayList<>();

    storageInfos.add(new StorageInfo(context.getFilesDir().getAbsolutePath(), true, false, -1));

    ArrayList<File> storageDirs = new ArrayList<>();
    File[] externalDirs = context.getExternalFilesDirs( null);

    for (File externalDir : externalDirs) {
        // "Returned paths may be null if a storage device is unavailable."
        if (externalDir == null) {
            continue;
        }

        String state = Environment.getStorageState(externalDir);
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            storageDirs.add(externalDir);
        }
    }

    for (File storageDir : storageDirs) {
        storageInfos.add(new StorageInfo(storageDir.getAbsolutePath(), false, false, -1));
    }

    return storageInfos;
}
 
Example 4
Source Project: FireFiles   File: HeatMapProvider.java    License: Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
private void updateVolumesLocked() {
    mRoots.clear();
    mIdToPath.clear();
    mIdToRoot.clear();
    
    int count = 0;
    StorageUtils storageUtils = new StorageUtils(getContext());
    for (StorageVolume volume : storageUtils.getStorageMounts()) {
        final File path = volume.getPathFile();
        if(Utils.hasKitKat()){
     	String state = Environment.getStorageState(path);
         final boolean mounted = Environment.MEDIA_MOUNTED.equals(state)
                 || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
         if (!mounted) continue;
        }
        final String rootId;
        if (volume.isPrimary() && volume.isEmulated()) {
            rootId = ROOT_ID_PRIMARY_EMULATED;
        } else if (volume.getUuid() != null) {
            rootId = ROOT_ID_SECONDARY + volume.getUserLabel();
        } else {
            Log.d(TAG, "Missing UUID for " + volume.getPath() + "; skipping");
            continue;
        }

        if (mIdToPath.containsKey(rootId)) {
            Log.w(TAG, "Duplicate UUID " + rootId + "; skipping");
            continue;
        }

        try {
        	if(null == path.listFiles()){
        		continue;
        	}
            mIdToPath.put(rootId, path);
            final RootInfo root = new RootInfo();
            
            root.rootId = rootId;
            root.flags = Document.FLAG_SUPPORTS_THUMBNAIL;
            if (ROOT_ID_PRIMARY_EMULATED.equals(rootId)) {
                root.title = getContext().getString(R.string.root_internal_storage);
            } else {
            	count++;
                root.title = getContext().getString(R.string.root_external_storage) + " " + count;// + volume.getLabel();
            }
            root.docId = getDocIdForFile(path);
            mRoots.add(root);
            mIdToRoot.put(rootId, root);
        } catch (FileNotFoundException e) {
            throw new IllegalStateException(e);
        }
    }

    Log.d(TAG, "After updating volumes, found " + mRoots.size() + " active roots");

    getContext().getContentResolver()
            .notifyChange(DocumentsContract.buildRootsUri(AUTHORITY), null, false);
}
 
Example 5
Source Project: FireFiles   File: HeatMapProvider.java    License: Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
private void updateVolumesLocked() {
    mRoots.clear();
    mIdToPath.clear();
    mIdToRoot.clear();
    
    int count = 0;
    StorageUtils storageUtils = new StorageUtils(getContext());
    for (StorageVolume volume : storageUtils.getStorageMounts()) {
        final File path = volume.getPathFile();
        if(Utils.hasKitKat()){
     	String state = Environment.getStorageState(path);
         final boolean mounted = Environment.MEDIA_MOUNTED.equals(state)
                 || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
         if (!mounted) continue;
        }
        final String rootId;
        if (volume.isPrimary() && volume.isEmulated()) {
            rootId = ROOT_ID_PRIMARY_EMULATED;
        } else if (volume.getUuid() != null) {
            rootId = ROOT_ID_SECONDARY + volume.getUserLabel();
        } else {
            Log.d(TAG, "Missing UUID for " + volume.getPath() + "; skipping");
            continue;
        }

        if (mIdToPath.containsKey(rootId)) {
            Log.w(TAG, "Duplicate UUID " + rootId + "; skipping");
            continue;
        }

        try {
        	if(null == path.listFiles()){
        		continue;
        	}
            mIdToPath.put(rootId, path);
            final RootInfo root = new RootInfo();
            
            root.rootId = rootId;
            root.flags = Document.FLAG_SUPPORTS_THUMBNAIL;
            if (ROOT_ID_PRIMARY_EMULATED.equals(rootId)) {
                root.title = getContext().getString(R.string.root_internal_storage);
            } else {
            	count++;
                root.title = getContext().getString(R.string.root_external_storage) + " " + count;// + volume.getLabel();
            }
            root.docId = getDocIdForFile(path);
            mRoots.add(root);
            mIdToRoot.put(rootId, root);
        } catch (FileNotFoundException e) {
            throw new IllegalStateException(e);
        }
    }

    Log.d(TAG, "After updating volumes, found " + mRoots.size() + " active roots");

    getContext().getContentResolver()
            .notifyChange(DocumentsContract.buildRootsUri(AUTHORITY), null, false);
}
 
Example 6
Source Project: FireFiles   File: HeatMapProvider.java    License: Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
private void updateVolumesLocked() {
    mRoots.clear();
    mIdToPath.clear();
    mIdToRoot.clear();
    
    int count = 0;
    StorageUtils storageUtils = new StorageUtils(getContext());
    for (StorageVolume volume : storageUtils.getStorageMounts()) {
        final File path = volume.getPathFile();
        if(Utils.hasKitKat()){
     	String state = Environment.getStorageState(path);
         final boolean mounted = Environment.MEDIA_MOUNTED.equals(state)
                 || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
         if (!mounted) continue;
        }
        final String rootId;
        if (volume.isPrimary() && volume.isEmulated()) {
            rootId = ROOT_ID_PRIMARY_EMULATED;
        } else if (volume.getUuid() != null) {
            rootId = ROOT_ID_SECONDARY + volume.getUserLabel();
        } else {
            Log.d(TAG, "Missing UUID for " + volume.getPath() + "; skipping");
            continue;
        }

        if (mIdToPath.containsKey(rootId)) {
            Log.w(TAG, "Duplicate UUID " + rootId + "; skipping");
            continue;
        }

        try {
        	if(null == path.listFiles()){
        		continue;
        	}
            mIdToPath.put(rootId, path);
            final RootInfo root = new RootInfo();
            
            root.rootId = rootId;
            root.flags = Document.FLAG_SUPPORTS_THUMBNAIL;
            if (ROOT_ID_PRIMARY_EMULATED.equals(rootId)) {
                root.title = getContext().getString(R.string.root_internal_storage);
            } else {
            	count++;
                root.title = getContext().getString(R.string.root_external_storage) + " " + count;// + volume.getLabel();
            }
            root.docId = getDocIdForFile(path);
            mRoots.add(root);
            mIdToRoot.put(rootId, root);
        } catch (FileNotFoundException e) {
            throw new IllegalStateException(e);
        }
    }

    Log.d(TAG, "After updating volumes, found " + mRoots.size() + " active roots");

    getContext().getContentResolver()
            .notifyChange(DocumentsContract.buildRootsUri(AUTHORITY), null, false);
}
 
Example 7
public static String getStorageState(File path) {
    return Environment.getStorageState(path);
}
 
Example 8
Source Project: V.FlyoutTest   File: EnvironmentCompatKitKat.java    License: MIT License 4 votes vote down vote up
public static String getStorageState(File path) {
    return Environment.getStorageState(path);
}
 
Example 9
Source Project: guideshow   File: EnvironmentCompatKitKat.java    License: MIT License 4 votes vote down vote up
public static String getStorageState(File path) {
    return Environment.getStorageState(path);
}