Example usage for android.os Environment isExternalStorageRemovable

List of usage examples for android.os Environment isExternalStorageRemovable

Introduction

In this page you can find the example usage for android.os Environment isExternalStorageRemovable.

Prototype

public static boolean isExternalStorageRemovable(File path) 

Source Link

Document

Returns whether the shared/external storage media at the given path is physically removable.

Usage

From source file:github.daneren2005.dsub.view.CacheLocationPreference.java

@Override
protected void onBindDialogView(View view) {
    super.onBindDialogView(view);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        view.setLayoutParams(new ViewGroup.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT));

        final EditText editText = (EditText) view.findViewById(android.R.id.edit);
        ViewGroup vg = (ViewGroup) editText.getParent();

        LinearLayout cacheButtonsWrapper = (LinearLayout) LayoutInflater.from(context)
                .inflate(R.layout.cache_location_buttons, vg, true);
        Button internalLocation = (Button) cacheButtonsWrapper.findViewById(R.id.location_internal);
        Button externalLocation = (Button) cacheButtonsWrapper.findViewById(R.id.location_external);

        File[] dirs;//from   w ww.ja  v a2 s.c  o m
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            dirs = context.getExternalMediaDirs();
        } else {
            dirs = ContextCompat.getExternalFilesDirs(context, null);
        }

        // Past 5.0 we can query directly for SD Card
        File internalDir = null, externalDir = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            for (int i = 0; i < dirs.length; i++) {
                try {
                    if (dirs[i] != null) {
                        if (Environment.isExternalStorageRemovable(dirs[i])) {
                            if (externalDir != null) {
                                externalDir = dirs[i];
                            }
                        } else {
                            internalDir = dirs[i];
                        }

                        if (internalDir != null && externalDir != null) {
                            break;
                        }
                    }
                } catch (Exception e) {
                    Log.e(TAG, "Failed to check if is external", e);
                }
            }
        }

        // Before 5.0, we have to guess.  Most of the time the SD card is last
        if (externalDir == null) {
            for (int i = dirs.length - 1; i >= 0; i--) {
                if (dirs[i] != null) {
                    externalDir = dirs[i];
                    break;
                }
            }
        }
        if (internalDir == null) {
            for (int i = 0; i < dirs.length; i++) {
                if (dirs[i] != null) {
                    internalDir = dirs[i];
                    break;
                }
            }
        }
        final File finalInternalDir = new File(internalDir, "music");
        final File finalExternalDir = new File(externalDir, "music");

        final EditText editTextBox = (EditText) view.findViewById(android.R.id.edit);
        if (finalInternalDir != null && (finalInternalDir.exists() || finalInternalDir.mkdirs())) {
            internalLocation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String path = finalInternalDir.getPath();
                    editTextBox.setText(path);
                }
            });
        } else {
            internalLocation.setEnabled(false);
        }

        if (finalExternalDir != null && !finalInternalDir.equals(finalExternalDir)
                && (finalExternalDir.exists() || finalExternalDir.mkdirs())) {
            externalLocation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String path = finalExternalDir.getPath();
                    editTextBox.setText(path);
                }
            });
        } else {
            externalLocation.setEnabled(false);
        }
    }
}

From source file:cordova.plugins.Diagnostic_External_Storage.java

/**
 * Returns all available external SD-Cards in the system.
 *
 * @return paths to all available external SD-Cards in the system.
 *///from w ww . j av a 2 s  . c o m
protected String[] getStorageDirectories() {

    List<String> results = new ArrayList<String>();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //Method 1 for KitKat & above
        File[] externalDirs = this.cordova.getActivity().getApplicationContext().getExternalFilesDirs(null);

        for (File file : externalDirs) {
            if (file == null) {
                continue;
            }
            String applicationPath = file.getPath();
            String rootPath = applicationPath.split("/Android")[0];

            boolean addPath = false;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                addPath = Environment.isExternalStorageRemovable(file);
            } else {
                addPath = Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(file));
            }

            if (addPath) {
                results.add(rootPath);
                results.add(applicationPath);
            }
        }
    }

    if (results.isEmpty()) { //Method 2 for all versions
        // better variation of: http://stackoverflow.com/a/40123073/5002496
        String output = "";
        try {
            final Process process = new ProcessBuilder().command("mount | grep /dev/block/vold")
                    .redirectErrorStream(true).start();
            process.waitFor();
            final InputStream is = process.getInputStream();
            final byte[] buffer = new byte[1024];
            while (is.read(buffer) != -1) {
                output = output + new String(buffer);
            }
            is.close();
        } catch (final Exception e) {
            e.printStackTrace();
        }
        if (!output.trim().isEmpty()) {
            String devicePoints[] = output.split("\n");
            for (String voldPoint : devicePoints) {
                results.add(voldPoint.split(" ")[2]);
            }
        }
    }

    //Below few lines is to remove paths which may not be external memory card, like OTG (feel free to comment them out)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        for (int i = 0; i < results.size(); i++) {
            if (!results.get(i).toLowerCase().matches(".*[0-9a-f]{4}[-][0-9a-f]{4}.*")) {
                diagnostic.logDebug(results.get(i) + " might not be extSDcard");
                results.remove(i--);
            }
        }
    } else {
        for (int i = 0; i < results.size(); i++) {
            if (!results.get(i).toLowerCase().contains("ext")
                    && !results.get(i).toLowerCase().contains("sdcard")) {
                diagnostic.logDebug(results.get(i) + " might not be extSDcard");
                results.remove(i--);
            }
        }
    }

    String[] storageDirectories = new String[results.size()];
    for (int i = 0; i < results.size(); ++i)
        storageDirectories[i] = results.get(i);

    return storageDirectories;
}

From source file:github.daneren2005.dsub.util.FileUtil.java

private static File getBestDir(File[] dirs) {
    // Past 5.0 we can query directly for SD Card
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        for (int i = 0; i < dirs.length; i++) {
            if (dirs[i] != null && Environment.isExternalStorageRemovable(dirs[i])) {
                return dirs[i];
            }//from   w w  w . j  a  v a  2 s  .c o  m
        }
    }

    // Before 5.0, we have to guess.  Most of the time the SD card is last
    for (int i = dirs.length - 1; i >= 0; i--) {
        if (dirs[i] != null) {
            return dirs[i];
        }
    }

    // Should be impossible to be reached
    return dirs[0];
}

From source file:github.popeen.dsub.util.FileUtil.java

private static File getBestDir(File[] dirs) {
    // Past 5.0 we can query directly for SD Card
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        for (int i = 0; i < dirs.length; i++) {
            try {
                if (dirs[i] != null && Environment.isExternalStorageRemovable(dirs[i])) {
                    return dirs[i];
                }/*from www  .  java2s. c  o  m*/
            } catch (Exception e) {
                Log.e(TAG, "Failed to check if is external", e);
            }
        }
    }

    // Before 5.0, we have to guess.  Most of the time the SD card is last
    for (int i = dirs.length - 1; i >= 0; i--) {
        if (dirs[i] != null) {
            return dirs[i];
        }
    }

    // Should be impossible to be reached
    return dirs[0];
}