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 String imageToLocal(Bitmap bitmap, String name) {
    String path = null;/*  w w w .  ja  va 2  s.c om*/
    try {
        if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            return null;
        }
        String filePath = Environment.getExternalStorageDirectory().getPath() + "/cache/";
        File file = new File(filePath, name);
        if (file.exists()) {
            path = file.getAbsolutePath();
            return path;
        } else {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();

        OutputStream outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();
        if (!bitmap.isRecycled()) {
            bitmap.recycle();
        }

        path = file.getAbsolutePath();
    } catch (Exception e) {
    }
    return path;
}

From source file:Main.java

public static boolean writeErrorLogToSDCard(String path, String errorLog) {
    if (!(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))) // check if SD card is mounted
        return false;

    File file;//from w w w.  ja  v a  2  s  .c  o m
    file = new File(path);
    if (!file.exists()) {
        try {
            file.getParentFile().mkdirs();
            file.createNewFile();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return false;
        }
    }

    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); //true means append to file
        writer.append(errorLog);
        writer.newLine();
        writer.append("-------------------------------"); // seperator
        writer.newLine();
        writer.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

public static File[] getMountedVolumesAsFile() {
    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 File[] { primaryExternalStorage });
        } else {//from  w w w . ja v a 2 s  . 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<File> data = new ArrayList<File>();

            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(file);
                    else {
                        if (!file.toString().contains("usb") || !file.toString().contains("USB")) {
                            data.add(file);
                        }
                    }
                }
            }
            return data.toArray(new File[data.size()]);
        }
    } else {
        // we cannot read the External Storage..., return null
        return null;
    }
}

From source file:Main.java

@SuppressLint("NewApi")
public static long getFreeDiskSpace() {
    String status = Environment.getExternalStorageState();
    long freeSpace = 0;
    if (status.equals(Environment.MEDIA_MOUNTED)) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long availableBlock = stat.getAvailableBlocksLong();
        freeSpace = availableBlock * blockSize / 1024;
    } else {/*from ww  w  .j  av  a  2 s  .c o m*/
        return -1;
    }
    return freeSpace;
}

From source file:Main.java

private static boolean sdCardIsExit() {
    return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}

From source file:Main.java

public static String getDownloadDir() {
    String status = Environment.getExternalStorageState();
    if (status == null || !status.equals(Environment.MEDIA_MOUNTED)) {
        return null;
    }/*w w w.  j  av  a  2  s .  c  o  m*/

    String path = null;

    // get the sdcard directory
    File sdFile = Environment.getExternalStorageDirectory();
    if (null != sdFile) {
        path = sdFile.toString();
    } else {
        path = "/sdcard/";
    }

    path += "/download";

    File destDir = new File(path);
    if (!destDir.exists()) {
        try {
            if (!destDir.mkdirs()) {
                Log.e("getDownloadDir", "create folder " + path + " failed");
                return null;
            }
        } catch (SecurityException e) {
            Log.e("getDownloadDir", "create folder " + path + " failed: " + e.toString());
            return null;
        }
    }

    return path;
}

From source file:Main.java

public static boolean saveToSDCard(Bitmap bitmap) {
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        return false;
    }//from w  ww. jav a2s .co  m
    FileOutputStream fileOutputStream = null;
    File file = new File("/sdcard/myName/Download/");
    if (!file.exists()) {
        file.mkdirs();
    }
    String fileName = UUID.randomUUID().toString() + ".jpg";
    String filePath = "/sdcard/myName/Download/" + fileName;
    File f = new File(filePath);
    if (!f.exists()) {
        try {
            f.createNewFile();
            fileOutputStream = new FileOutputStream(filePath);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
        } catch (IOException e) {
            return false;
        } finally {
            try {
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (IOException e) {
                return false;
            }
        }
    }
    return true;
}

From source file:Main.java

public static boolean isExternalStorageWriteable() {
    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        mExternalStorageAvailable = mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {//from  www. j  a  v  a2 s  .  c  o m
        // 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
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }

    return mExternalStorageAvailable & mExternalStorageWriteable;
}

From source file:Main.java

private static String getStorageDirectory() {
    return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ? sdRootPath + FOLDER_NAME
            : appRootPath + FOLDER_NAME;
}

From source file:Main.java

public static boolean storeBitmapToFile(Bitmap bitmap, String filePath) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        try {/*from   w ww  .  j  av a  2  s  .co m*/
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
            bitmap.compress(CompressFormat.PNG, 50, bos);
            bos.flush();
            bos.close();
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    return false;
}