Example usage for android.os Environment MEDIA_MOUNTED

List of usage examples for android.os Environment MEDIA_MOUNTED

Introduction

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

Prototype

String MEDIA_MOUNTED

To view the source code for android.os Environment MEDIA_MOUNTED.

Click Source Link

Document

Storage state if the media is present and mounted at its mount point with read/write access.

Usage

From source file:Main.java

public static boolean isExternalStorageWriteable() {
    boolean writealbe = false;
    long start = System.currentTimeMillis();
    if (TextUtils.equals(Environment.MEDIA_MOUNTED, Environment.getExternalStorageState())) {
        File esd = Environment.getExternalStorageDirectory();
        if (esd.exists() && esd.canWrite()) {
            File file = new File(esd, ".696E5309-E4A7-27C0-A787-0B2CEBF1F1AB");
            if (file.exists()) {
                writealbe = true;//  w  w  w .  j  a v  a 2s.c o m
            } else {
                try {
                    writealbe = file.createNewFile();
                } catch (IOException e) {
                    Log.w(TAG, "isExternalStorageWriteable() can't create test file.");
                }
            }
        }
    }
    long end = System.currentTimeMillis();
    Log.i(TAG, "Utility.isExternalStorageWriteable(" + writealbe + ") cost " + (end - start) + "ms.");
    return writealbe;
}

From source file:Main.java

/**
 * Helper Method to Test if external Storage is Available from
 * http://www.ibm.com/developerworks/xml/library/x-androidstorage/index.html
 *//*  w ww . j av  a2 s  . co  m*/
public static boolean isExternalStorageAvailable() {
    boolean state = false;
    String extStorageState = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
        state = true;
    }
    return state;
}

From source file:Main.java

public static boolean writeFile(byte[] buffer, String folder, String fileName) {
    boolean writeSucc = false;

    boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

    String folderPath = "";
    if (sdCardExist) {
        folderPath = Environment.getExternalStorageDirectory() + File.separator + folder + File.separator;
    } else {//from  ww w .ja  v  a2 s .  c o  m
        writeSucc = false;
    }

    File fileDir = new File(folderPath);
    if (!fileDir.exists()) {
        fileDir.mkdirs();
    }

    File file = new File(folderPath + fileName);
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(file);
        out.write(buffer);
        writeSucc = true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return writeSucc;
}

From source file:Main.java

public static void saveWeiboBitmap(Bitmap bitmap, String filename) {
    String status = Environment.getExternalStorageState();
    if (bitmap == null || !status.equals(Environment.MEDIA_MOUNTED)) {
        return;//from w w  w. ja v a2 s  .  com
    }
    File f = new File(filename);
    FileOutputStream fOut = null;
    boolean error = false;
    try {
        f.createNewFile();
    } catch (IOException e1) {
        e1.printStackTrace();
        error = true;
    }

    try {
        fOut = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        error = true;
    }

    try {
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    } catch (Exception e) {
        e.printStackTrace();
        error = true;
    }

    try {
        fOut.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

private static boolean isHasSdcard() {
    String status = Environment.getExternalStorageState();
    if (status.equals(Environment.MEDIA_MOUNTED)) {
        return true;
    } else {//from w ww . j  a va 2  s. com
        return false;
    }
}

From source file:Main.java

public static String getStorePath() {
    if (mStorePath == null) {
        String path = Environment.getExternalStorageDirectory().getPath();
        if (path == null || !Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            path = mContext.getFilesDir().getPath();
        }//w w w .  j  a  v a 2 s .  co  m
        if (!path.endsWith("/")) {
            path = path + "/";
        }
        mStorePath = path;
    }
    return mStorePath;
}

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

public static boolean writeFile(byte[] buffer, String folder, String fileName) {
    boolean writeSucc = false;
    boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    String folderPath = "";
    if (sdCardExist) {
        folderPath = Environment.getExternalStorageDirectory() + File.separator + folder + File.separator;
    } else {//from ww w  . j  a  v a2 s  .co m
        writeSucc = false;
    }

    File fileDir = new File((folderPath));
    if (!fileDir.exists()) {
        fileDir.mkdirs();
    }

    File file = new File(folderPath + fileName);
    FileOutputStream out = null;

    try {
        out = new FileOutputStream(file);
        out.write(buffer);
        writeSucc = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return writeSucc;
}

From source file:Main.java

public static boolean checkSdCardIsExist() {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        return true;
    } else {/*w  ww  .  j a  va 2s. c  o  m*/
        return false;
    }
}

From source file:Main.java

public static boolean hasMainSDCard() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        //            boolean writable = checkFsWritable();
        //            return writable;
        return true;
    }//from   w  w w  . ja  v a2  s  . c  o m
    return false;
}