Example usage for android.os Environment getExternalStorageDirectory

List of usage examples for android.os Environment getExternalStorageDirectory

Introduction

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

Prototype

public static File getExternalStorageDirectory() 

Source Link

Document

Return the primary shared/external storage directory.

Usage

From source file:Main.java

public static boolean deleteDirectory(String fileName) {
    boolean status;
    SecurityManager checker = new SecurityManager();

    if (!fileName.equals("")) {

        File path = Environment.getExternalStorageDirectory();
        File newPath = new File(path.toString() + fileName);
        checker.checkDelete(newPath.toString());
        if (newPath.isDirectory()) {
            String[] listfile = newPath.list();
            // delete all files within the specified directory and then
            // delete the directory
            try {
                for (int i = 0; i < listfile.length; i++) {
                    File deletedFile = new File(newPath.toString() + "/" + listfile[i].toString());
                    deletedFile.delete();
                }//w  w w  . j a  v  a2s  .  c  om
                newPath.delete();
                Log.d(TAG, "DirectoryManager deleteDirectory" + fileName);
                status = true;
            } catch (Exception e) {
                e.printStackTrace();
                status = false;
            }

        } else
            status = false;
    } else
        status = false;
    return status;
}

From source file:Main.java

public static void appendLog(String text) {
    File logFile = new File(Environment.getExternalStorageDirectory() + "/clr_log.file");
    if (!logFile.exists()) {
        try {/* ww w  .j a va 2  s .co  m*/
            logFile.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    try {
        BufferedReader buf = new BufferedReader(new FileReader(logFile));

        String line = "";
        ArrayList<String> lines = new ArrayList<String>();

        while ((line = buf.readLine()) != null) {
            lines.add(line);
        }

        int size = lines.size();

        //Every time the number of lines go over 1000, only add the last 500. This will keep its size to a minimum
        int i = lines.size() - 500;
        if (i < 0) {
            i = 0;
        }

        buf.close();

        BufferedWriter bufW = new BufferedWriter(new FileWriter(logFile, true));

        if (size > 1000) {
            bufW.write("");
            for (; i < lines.size(); i++) {
                bufW.append(line);
            }
        }

        bufW.append(text + "\n");
        bufW.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String getGlideDefaultPath(Context context) {
    if (context == null) {
        throw new RuntimeException("context can't be null");
    }//from   ww  w  . ja v a 2s  .c o  m
    String path = context.getCacheDir().getAbsolutePath();
    if (isSDCard()) {
        String directory = Environment.getExternalStorageDirectory() + "/GankLy/cache/img";
        File file = new File(directory);
        if (!file.exists()) {
            if (file.mkdirs()) {
                return directory;
            }
        }
    }
    return path;
}

From source file:Main.java

/**
 * Returns true if the stoarge is almost full which indicates that the user probably needs to
 * free up some space./*w  w  w  .  j  av a  2  s . c o m*/
 */
public static boolean isStorageAlmostFull() {
    return Environment.getExternalStorageDirectory().getUsableSpace() < STORAGE_ALMOST_FULL_THRESHOLD_BYTES;
}

From source file:Main.java

/**
 * Get the SD card directory.//from   w w  w .  jav  a 2  s  . c om
 *
 * @return The SD card directory.
 */
@NonNull
public static String getSdCardPath() {
    String sdCardDirectory = Environment.getExternalStorageDirectory().getAbsolutePath();

    try {
        sdCardDirectory = new File(sdCardDirectory).getCanonicalPath();
    } catch (IOException ioe) {
        Log.e("Uri", "Could not get SD directory", ioe);
    }
    return sdCardDirectory;
}

From source file:Main.java

public static File getFileFromBytes(String name, String path) {
    byte[] b = name.getBytes();
    BufferedOutputStream stream = null;
    File file = null;//w  w w .j av  a2  s.c om
    try {
        file = new File(Environment.getExternalStorageDirectory() + path);
        FileOutputStream fstream = new FileOutputStream(file);
        stream = new BufferedOutputStream(fstream);
        stream.write(b);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    return file;
}

From source file:Main.java

private static boolean checkFsWritable() {
    // Create a temporary file to see whether a volume is really writeable.
    // It's important not to put it in the root directory which may have a
    // limit on the number of files.
    String directoryName = Environment.getExternalStorageDirectory().toString() + "/DCIM";
    File directory = new File(directoryName);
    if (!directory.isDirectory()) {
        if (!directory.mkdirs()) {
            return false;
        }/*ww  w  .  j  a v  a2 s  .  c  o  m*/
    }
    File f = new File(directoryName, ".probe");
    try {
        // Remove stale file if any
        if (f.exists()) {
            f.delete();
        }
        if (!f.createNewFile()) {
            return false;
        }
        f.delete();
        return true;
    } catch (IOException ex) {
        return false;
    }
}

From source file:Main.java

public static Uri createUri(String foldername) {
    String storageState = Environment.getExternalStorageState();
    if (storageState.equals(Environment.MEDIA_MOUNTED)) {
        final File root = new File(
                Environment.getExternalStorageDirectory() + File.separator + foldername + File.separator);
        root.mkdirs();/*  w  ww  .  j  av  a2  s . c  o m*/
        final String fname = getUniqueImageFilename(foldername);
        final File sdImageMainDirectory = new File(root, fname);
        return Uri.fromFile(sdImageMainDirectory);
    }
    return null;
}

From source file:Main.java

public static long getFreeSpaceOnSd() {
    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
    //double sdFreeMB = ((double)stat.getAvailableBlocks() * (double) stat.getBlockSize()) / MB;
    return stat.getAvailableBlocks() * stat.getBlockSize();
}

From source file:Main.java

public static String getSaveImagePath(Context context) {
    String path = getCacheDir(context).getAbsolutePath();
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        path = Environment.getExternalStorageDirectory().getAbsolutePath();
    }/*w w  w .ja va 2 s .  c o  m*/

    path = path + File.separator + "Pictures";
    File file = new File(path);
    if (!file.exists()) {
        file.mkdir();
    }
    return path;
}