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:com.tcl.lzhang1.mymusic.AppException.java

/**
 * Save log.//www. jav a 2 s.c  o  m
 * 
 * @param ex the ex
 */
private void saveLog(Throwable ex) {
    String errorlog = "errorlog.txt";
    String savePath = "";
    String logFilePath = "";
    FileWriter fw = null;
    PrintWriter pw = null;
    try {

        String storageState = Environment.getExternalStorageState();
        if (storageState.equals(Environment.MEDIA_MOUNTED)) {
            savePath = Environment.getExternalStorageDirectory().getAbsolutePath()
                    + "/Android/data/com.tcl.lzhang1.mymusic/Log/";
            File file = new File(savePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            logFilePath = savePath + errorlog;
        }

        if (logFilePath == "") {
            return;
        }
        File logFile = new File(logFilePath);
        if (!logFile.exists()) {
            logFile.createNewFile();
        }
        fw = new FileWriter(logFile, true);
        pw = new PrintWriter(fw);
        pw.println("--------------------" + (new Date().toLocaleString())
                + "-------------------setDefaultUncaughtExceptionHandler--");
        ex.printStackTrace(pw);
        pw.close();
        fw.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (pw != null) {
            pw.close();
        }
        if (fw != null) {
            try {
                fw.close();
            } catch (IOException e) {
            }
        }
    }

}

From source file:com.todotxt.todotxttouch.util.Util.java

public static boolean isDeviceWritable() {
    String sdState = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(sdState)) {
        return true;
    } else {/*from  w  w  w . ja v a2 s .c  om*/
        return false;
    }
}

From source file:com.todotxt.todotxttouch.util.Util.java

public static boolean isDeviceReadable() {
    String sdState = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(sdState) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(sdState)) {
        return true;
    } else {//  w w  w  .  j ava2  s .com
        return false;
    }
}

From source file:com.phonegap.customcamera.NativeCameraLauncher.java

private String getTempDirectoryPath(Context ctx) {
    File cache = null;/*from w ww .j av a  2 s  .co m*/

    // SD Card Mounted
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        cache = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/"
                + ctx.getPackageName() + "/cache/");
    }
    // Use internal storage
    else {
        cache = ctx.getCacheDir();
    }

    // Create the cache directory if it doesn't exist
    if (!cache.exists()) {
        cache.mkdirs();
    }

    return cache.getAbsolutePath();
}

From source file:com.akop.bach.ImageCache.java

public File getCacheDirectory() {
    File cacheDir = mCacheDir;//  w w w. j  av  a  2 s  .  c o  m

    if (mSdCacheDir != null && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        if (mSdCacheDir.exists() || mSdCacheDir.mkdirs())
            cacheDir = mSdCacheDir;
    }

    return cacheDir;
}

From source file:io.strider.camera.CameraLauncher.java

public void cleanCache() {
    File cache = null;/*from  w  w w  . j  a v a  2 s . com*/

    // SD Card Mounted
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        cache = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/"
                + cordova.getActivity().getPackageName() + "/cache/");
    }
    // Use internal storage
    else {
        cache = cordova.getActivity().getCacheDir();
    }

    // Empty Cache folder
    if (cache.exists()) {
        File[] files = cache.listFiles();
        for (int i = 0; i < files.length; i++) {
            files[i].delete();
        }
    }

}

From source file:com.androidzeitgeist.dashwatch.common.IOUtil.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static File getBestAvailableCacheRoot(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // In KitKat we can query multiple devices.
        // TODO: optimize for stability instead of picking first one
        File[] roots = context.getExternalCacheDirs();
        if (roots != null) {
            for (File root : roots) {
                if (root == null) {
                    continue;
                }//ww  w.j  av  a  2 s . c  om

                if (Environment.MEDIA_MOUNTED.equals(Environment.getStorageState(root))) {
                    return root;
                }
            }
        }

    } else if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        // Pre-KitKat, only one external storage device was addressable
        return context.getExternalCacheDir();
    }

    // Worst case, resort to internal storage
    return context.getCacheDir();
}

From source file:org.jinzora.util.DrawableManager.java

private boolean externalStorageAvailable() {
    String state = Environment.getExternalStorageState();
    return (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state));
}

From source file:com.ichi2.anki.AnkiDroidApp.java

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

From source file:com.matthewmitchell.peercoin_android_wallet.ui.WalletActivity.java

@Override
public boolean onPrepareOptionsMenu(final Menu menu) {

    super.onPrepareOptionsMenu(menu);

    if (!application.isLoaded())
        return false; // Wallet not loaded just yet

    final Resources res = getResources();
    final String externalStorageState = Environment.getExternalStorageState();

    boolean writable = Environment.MEDIA_MOUNTED.equals(externalStorageState);
    boolean readOnly = Environment.MEDIA_MOUNTED_READ_ONLY.equals(externalStorageState);

    menu.findItem(R.id.wallet_options_exchange_rates)
            .setVisible(res.getBoolean(R.bool.show_exchange_rates_option));
    menu.findItem(R.id.wallet_options_restore_wallet).setEnabled(writable || readOnly);
    menu.findItem(R.id.wallet_options_backup_wallet).setEnabled(writable);
    menu.findItem(R.id.wallet_options_export)
            .setEnabled(writable && txListAdapter != null && !txListAdapter.transactions.isEmpty());

    menu.findItem(R.id.wallet_options_encrypt_keys)
            .setTitle(application.getWallet().isEncrypted() ? R.string.wallet_options_encrypt_keys_change
                    : R.string.wallet_options_encrypt_keys_set);

    return true;//from  ww  w . ja v a 2  s  .co  m

}