Example usage for android.os Environment getExternalStorageState

List of usage examples for android.os Environment getExternalStorageState

Introduction

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

Prototype

public static String getExternalStorageState() 

Source Link

Document

Returns the current state of the primary shared/external storage media.

Usage

From source file:Main.java

public static void init(Context context) {
    voice_wechat_paths = new ArrayList<>();
    voice_qq_paths = new ArrayList<>();

    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        File dir = Environment.getExternalStorageDirectory();
        File f = new File(dir + "/tencent/MicroMsg");

        if (f.exists() && f.canRead() && f.isDirectory()) {
            File[] files = f.listFiles();
            if (files == null || files.length == 0) {
                return;
            }//from  ww  w. j  a  v a  2  s  .co  m

            for (File f0 : files) {
                if (f0.isDirectory() && f0.getName().length() > 24) {
                    voice_wechat_paths.add(f0.getAbsolutePath() + "/voice2");
                }
            }
        }

        File exportDir = new File(dir, "silkv3_mp3");
        if (!exportDir.exists()) {
            exportDir.mkdirs();
        }

        export_dir = exportDir.getAbsolutePath();
    }
}

From source file:Main.java

/**
 * // w w  w.  ja va 2  s .c  o m
 * @param subDirName
 * @return
 */
public static String[] constructFileList(String subDirName) {
    List<String> fileList = null;

    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        File subDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
                + DIR_NAME + File.separator + subDirName);

        if (subDir.exists()) {
            File[] files = subDir.listFiles();

            for (int i = 0; i < files.length; i++) {
                if (fileList == null) {
                    fileList = new ArrayList<String>();
                }

                // remove the extension
                fileList.add(files[i].getName().split("\\.")[0]);
            }
        } else {
            Log.e(LOG_TAG, "Nothing to import");
        }
    }

    return fileList.toArray(new String[fileList.size()]);
}

From source file:Main.java

public static boolean isCardExist() {
    return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}

From source file:Main.java

public static File getDiskCacheDir(Context context, String uniqueName) {
    // Check if media is mounted or storage is built-in, if so, try and use
    // external cache dir
    // otherwise use internal cache dir

    final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !isExternalStorageRemovable() ? getExternalCacheDir(context).getPath()
                    : context.getCacheDir().getPath();

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

From source file:Main.java

/**
 * Get a writeable cache directory for saving cache files
 *
 * @param context/*  w  ww .  j  a v  a 2  s.co  m*/
 * @return file directory or null
 */
public static File getApplicationCacheDirectory(Context context) {
    File directory = context.getFilesDir();

    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        File externalDirectory = context.getExternalFilesDir(null);
        if (externalDirectory != null) {
            directory = externalDirectory;
        }
    }

    File cacheDirectory = new File(directory, CACHE_DIRECTORY);
    if (!cacheDirectory.exists()) {
        cacheDirectory.mkdir();
    }

    return cacheDirectory;
}

From source file:Main.java

public static boolean saveBitmapToLocal(String path, String fileName, Bitmap b) {
    if (b == null) {
        return false;
    }/*from   w  w w .  j  a v a  2 s.  c  o  m*/

    boolean result = false;
    String storageState = Environment.getExternalStorageState();
    if (storageState.equals(Environment.MEDIA_MOUNTED)) {

        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(path + fileName);
            b.compress(CompressFormat.JPEG, 100, fos);
            fos.flush();
            result = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
    return result;
}

From source file:Main.java

public static boolean isSdCardExist() {
    return Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}

From source file:Main.java

public static String getCacheDirectory() {
    String cachedDir = null;/*from w w  w  .  ja  v a  2s. c  om*/
    if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && hasExternalStoragePermission()) {
        cachedDir = getExternalCacheDir().getAbsolutePath();
    }
    if (cachedDir == null) {
        cachedDir = mContext.getCacheDir().getAbsolutePath();
    }
    if (cachedDir == null) {
        cachedDir = mContext.getFilesDir().getParentFile().getPath() + mContext.getPackageName() + "/cache";
    }
    return cachedDir;
}

From source file:Main.java

public static boolean isExternalStorageWritable() {
    // get the external storage condition
    String state = Environment.getExternalStorageState();

    // if writable
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }/*ww  w.  j a  v a2s  .  c  om*/

    return false;
}

From source file:Main.java

/**
 * Get disk cache directory/* w ww  . j av a  2  s  . 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);
}