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 void savePic2SD(Bitmap bitmap, String path, String folder) {

    boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    if (sdCardExist) {
        File fileDir = new File(folder);
        if (!fileDir.exists()) {
            fileDir.mkdir();/*ww  w  .j a v a 2  s . c o m*/
        }
    }

    File file = new File(path);
    try {
        FileOutputStream out = new FileOutputStream(file);

        if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
            out.flush();
            out.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void savePic2SD(Bitmap bitmap, String path, String folder) {

    boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    if (sdCardExist) {
        File fileDir = new File(folder);
        if (!fileDir.exists()) {
            fileDir.mkdir();/*from w w  w  . j  a v a2  s  . com*/
        }
    }

    File file = new File(path);
    if (file.exists()) {
        file.delete();
    }
    try {
        FileOutputStream out = new FileOutputStream(file);

        if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
            out.flush();
            out.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * Get the external storage path of the device
 *
 * @return The external storage path of the device.
 *///from www.j  a  v  a  2  s  .co  m
public static String getAppRootDirectory() {
    try {
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return null;
        }
    } catch (Exception e) {
        // Catch exception is trying to fix a crash inside of Environment.getExternalStorageState().
        e.printStackTrace();
        return null;
    }
    String rootDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + ROOT_DIR + "/";
    File file = new File(rootDir);
    if (!file.exists()) {
        if (!file.mkdirs()) {
            return null;
        }
    }
    return rootDir;
}

From source file:Main.java

public static boolean isExternalStoragePresent(Context context) {
    boolean externalStorageAvailable;
    boolean externalStorageWritable;
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        externalStorageAvailable = externalStorageWritable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
        externalStorageAvailable = true;
        externalStorageWritable = false;
    } else {//from w ww . j a  va2 s .  c om
        // Something else is wrong. It may be one of many other states, but all we need to know is we can neither read nor write
        externalStorageAvailable = externalStorageWritable = false;
    }
    if (!((externalStorageAvailable) && (externalStorageWritable))) {
        Toast.makeText(context, "SD card not present", Toast.LENGTH_LONG).show();
    }
    return (externalStorageAvailable) && (externalStorageWritable);
}

From source file:Main.java

public static boolean checkSDCardAvailabel() {
    return Environment.getExternalStorageDirectory().equals(Environment.MEDIA_MOUNTED);
}

From source file:Main.java

public static int saveToSdCard(String fileName, Bitmap bitmap) {
    int ret = 0;/*from   w  ww  .j a v a  2 s  .  c om*/
    PrintStream out = null;
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        return -1;
    }
    File file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + fileName);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdir();
    }
    try {
        out = new PrintStream(new FileOutputStream(file));
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        ret = -2;
    } finally {
        out.flush();
        out.close();
        if (!bitmap.isRecycled())
            bitmap.recycle();
    }

    return ret;
}

From source file:Main.java

public static boolean isSDCardExist() {
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
        return true;
    } else/*from   w  w  w  .  j  a v a2s .co m*/
        return false;
}

From source file:Main.java

public static boolean isExternalMemoryWritable() {
    String externalStorageState = Environment.getExternalStorageState();
    return externalStorageState.equals(Environment.MEDIA_MOUNTED);
}

From source file:Main.java

public static boolean isExistExternalStore() {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        return true;
    } else {//from w  ww. j  ava2s  . co  m
        return false;
    }
}

From source file:Main.java

public static String compressGzipFile(String filename) {
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

        File flockedFilesFolder = new File(
                Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
        System.out.println("FlockedFileDir: " + flockedFilesFolder);
        String uncompressedFile = flockedFilesFolder.toString() + "/" + filename;
        String compressedFile = flockedFilesFolder.toString() + "/" + "gzip.zip";

        try {/*from w  ww  . j  av  a2 s . c om*/
            FileInputStream fis = new FileInputStream(uncompressedFile);
            FileOutputStream fos = new FileOutputStream(compressedFile);
            GZIPOutputStream gzipOS = new GZIPOutputStream(fos) {
                {
                    def.setLevel(Deflater.BEST_COMPRESSION);
                }
            };

            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                gzipOS.write(buffer, 0, len);
            }
            //close resources
            gzipOS.close();
            fos.close();
            fis.close();
            return compressedFile;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;

}