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() 

Source Link

Document

Returns whether the primary shared/external storage media is physically removable.

Usage

From source file:Main.java

/**
 * Get disk cache directory//from w w w. ja  v a2s  . c o  m
 *
 * @param ctx
 * @param uniqueName Unique name for caching directory, use it separate different types of cached files. eg: bitmap,strings,css,files etc.
 * @return cache directory
 */
public static File getDiskCacheDir(Context ctx, String uniqueName) {

    String cachePath;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        cachePath = ctx.getExternalCacheDir().getPath();
    } else {
        cachePath = ctx.getCacheDir().getPath();
    }

    return new File(cachePath, uniqueName);
}

From source file:Main.java

public static String getCacheDir() {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        File cacheFile = mAppContext.getExternalCacheDir();
        if (null != cacheFile) {
            return cacheFile.getPath();
        }/*from   w w w .j a v a2s .  c om*/
    }
    return mAppContext.getCacheDir().getPath();
}

From source file:Main.java

public static String[] getMountedVolumes() {
    final String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // we can read the External Storage...           
        //Retrieve the primary External Storage:
        final File primaryExternalStorage = Environment.getExternalStorageDirectory();

        //Retrieve the External Storages root directory:
        String externalStorageRootDir = null;
        if ((externalStorageRootDir = primaryExternalStorage.getParent()) == null) { // no parent...
            return (new String[] { "ONLY A SINGLE VOLUME HAS BEEN DETECTED!",
                    (Environment.isExternalStorageRemovable() ? "(REMOVABLE SD-CARD)" : "(INTERNAL STORAGE)")
                            + " PRIMARY STORAGE: " + primaryExternalStorage });
        } else {//from w w w. j a  v  a2s. c  o  m
            final File externalStorageRoot = new File(externalStorageRootDir);
            final File[] files = externalStorageRoot.listFiles(new FilenameFilter() {

                @Override
                public boolean accept(File dir, String filename) {
                    // TODO Auto-generated method stub

                    File file = new File(dir, filename);
                    if (file.isDirectory() && file.canRead() && file.canWrite() && !file.isHidden()) {
                        return true;
                    }
                    return false;
                }

            }); //.listFiles();
            List<String> data = new ArrayList<String>();
            if (files.length > 1) {
                data.add("MULTIPLE VOLUMES HAS BEEN DETECTED!");
                data.add("Enumerating detected volumes . . .");
            } else {
                data.add("ONLY A SINGLE VOLUME HAS BEEN DETECTED!");
            }

            for (final File file : files) {
                if (file.isDirectory() && file.canRead() && file.canWrite() && !file.isHidden()
                        && (files.length > 0)) { // it is a real directory (not a USB drive)...
                    if (file.toString().equals(primaryExternalStorage.toString()))
                        data.add((Environment.isExternalStorageRemovable() ? "(REMOVABLE SD-CARD)"
                                : "(INTERNAL Memory)") + " PRIMARY STORAGE: " + file.getAbsolutePath());
                    else {
                        data.add(((file.toString().contains("usb") || file.toString().contains("USB"))
                                ? "MOUNTED USB"
                                : "MOUNTED") + " STORAGE: " + file.getAbsolutePath());
                    }
                }
            }
            return data.toArray(new String[data.size()]);
        }
    } else {
        // we cannot read the External Storage..., return null
        return null;
    }

}

From source file:Main.java

/**
 * Check if external storage is built-in or removable.
 *
 * @return True if external storage is removable (like an SD card), false
 * otherwise./*  ww w .  j  ava2s . c om*/
 */
public static boolean isExternalStorageRemovable() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        return Environment.isExternalStorageRemovable();
    }
    return true;
}

From source file:Main.java

/**
 * Check if external storage is built-in or removable.
 *
 * @return True if external storage is removable (like an SD card), false
 *         otherwise.//from w w  w. java2 s.  c  o  m
 */
@TargetApi(9)
public static boolean isExternalStorageRemovable() {
    if (Build.VERSION.SDK_INT >= 9) {
        return Environment.isExternalStorageRemovable();
    }
    return true;
}

From source file:Main.java

/**
 * Check if external storage is built-in or removable.
 *
 * @return True if external storage is removable (like an SD card), false
 *         otherwise.//from  w  w w.j  a v  a  2  s.c  o  m
 */
@SuppressLint("NewApi")
public static boolean isExternalStorageRemovable() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        return Environment.isExternalStorageRemovable();
    }
    return true;
}

From source file:Main.java

/**
 * //from  w  w w  .  j av  a  2 s  .c  o  m
 * @return
 */
static boolean isExternalStorageRemovable() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        return Environment.isExternalStorageRemovable();
    }
    return true;
}

From source file:org.xwalk.runtime.extension.api.device_capabilities.DeviceCapabilitiesStorage.java

private void initStorageList() {
    mStorageList.clear();//from  w ww . j a  v  a2 s . c  o  m
    mStorageCount = 0;

    StorageUnit unit = new StorageUnit(mStorageCount, "Internal", "fixed");
    unit.setPath(Environment.getRootDirectory().getAbsolutePath());
    mStorageList.put(mStorageCount, unit);
    ++mStorageCount;

    // Attempt to add emulated stroage first
    int sdcardNum = mStorageCount - 1; // sdcard count from 0
    unit = new StorageUnit(mStorageCount, new String("sdcard" + Integer.toString(sdcardNum)), "fixed");
    if (Environment.isExternalStorageRemovable()) {
        unit.setType("removable");
    }
    unit.setPath(Environment.getExternalStorageDirectory().getAbsolutePath());

    if (unit.isValid()) {
        mStorageList.put(mStorageCount, unit);
        ++mStorageCount;
    }

    // Then attempt to add real removable storage
    attemptAddExternalStorage();
}

From source file:com.android.volley.misc.Utils.java

@SuppressLint("NewApi")
private static boolean isExternalMounted() {
    if (Utils.hasGingerbread()) {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable();
    }//w  w  w  .  j av a 2s  . c  o m
    return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}

From source file:com.isaacrf.epicbitmaprenderer.core.EpicBitmapCache.java

/**
 * Creates a unique subdirectory of the designated app cache directory. Tries to use external
 * but if not mounted, falls back on internal storage.
 *
 * @param context    Context from where EpicBitmapCache is being used.
 * @param uniqueName Unique name for cache directory.
 * @return {@link File} pointing to cache directory
 *///from  w  w w .  ja  v  a  2  s.c o  m
public static File getDiskCacheDir(Context context, String uniqueName) {
    File externalCacheDir = context.getExternalCacheDir();
    File cacheDir = context.getCacheDir();
    String cachePath;

    // Check if media is mounted or storage is built-in, if so, try and use external cache dir
    // otherwise use internal cache dir
    if ((Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) && externalCacheDir != null) {
        cachePath = externalCacheDir.getPath();
    } else {
        cachePath = cacheDir.getPath();
    }

    return new File(cachePath + File.separator + uniqueName);
}