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 String getDownloadDir() {
    String status = Environment.getExternalStorageState();
    if (status == null || !status.equals(Environment.MEDIA_MOUNTED)) {
        return null;
    }// www .  ja  v a 2  s  .  c om

    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

private static boolean isAvailableFileSystem(String fileSystemName) {
    final String[] unAvailableFileSystemList = { "/dev", "/mnt/asec", "/mnt/obb", "/system", "/data", "/cache",
            "/efs", "/firmware" };

    for (String name : unAvailableFileSystemList) {
        if (fileSystemName.contains(name) == true) {
            return false;
        }/*from w  w  w.  ja v a 2s . c o m*/
    }

    if (Environment.getExternalStorageDirectory().getAbsolutePath().equals(fileSystemName) == true) {
        return false;
    }

    return true;
}

From source file:Main.java

public static String[] listNarDir() {
    File narDir = new File(Environment.getExternalStorageDirectory(), "nar");
    if (narDir.exists() == false || narDir.isDirectory() == false)
        return null;

    String[] ret = narDir.list(narFilter);
    return ret;/*from   ww  w.j a  v a2  s .c  om*/
}

From source file:Main.java

public static int saveToSdCard(String fileName, Bitmap bitmap) {
    int ret = 0;//from   w w  w . j  a  va  2s . com
    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

/**
 * Forces the Android gallery to  refresh its thumbnail images.
 * @param context/* ww w .  j ava 2  s. co  m*/
 * @param fdelete
 */
private static void refreshGalleryImages(Context context, File fdelete) {
    try {
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://" + Environment.getExternalStorageDirectory())));
    } catch (Exception e1) {
        try {
            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri contentUri = Uri.fromFile(fdelete);
            mediaScanIntent.setData(contentUri);
            context.sendBroadcast(mediaScanIntent);
        } catch (Exception e2) {
        }
    }
}

From source file:Main.java

/**
 * get the FileOutputStream//from  w  w w .ja  v a  2 s  .c o  m
 * @param filePath the filePath must contain head "/"
 * @return
 */
public static FileOutputStream getFileOutputStream(String filePath) {
    FileOutputStream fouts = null;
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + filePath.trim());

    if (file.exists() && file.isFile()) {
        try {
            fouts = new FileOutputStream(file);
            Log.d("Ragnarok", "get the fouts path = " + file.getAbsolutePath());
            return fouts;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        try {
            File fileDirs = new File(file.getParent());
            fileDirs.mkdirs();
            //Log.d(LOG_TAG, "make the fileDirs " + fileDirs.getPath());
            //file.createNewFile();   
            //Log.d("Ragnarok", "create a new file name " + file.getName());
            Log.d("Ragnarok", "file path " + file.getAbsolutePath());
            synchronized (file) {
                file.createNewFile();
                fouts = new FileOutputStream(file);
                return fouts;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    return null;
}

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 ww  .jav 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

public static Boolean writeToSDFile(String directory, String file_name, String text) {

    // Find the root of the external storage.
    // See//from w  w  w  . j av a  2 s .  c  o  m
    // http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

    File root = Environment.getExternalStorageDirectory();

    // See
    // http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder

    File dir = new File(root.getAbsolutePath() + "/" + directory);
    dir.mkdirs();
    File file = new File(dir, file_name);

    try {
        FileOutputStream f = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(f);
        pw.println(text);
        pw.flush();
        pw.close();
        f.close();
        // Log.v(TAG, "file written to sd card");
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        // Log.i(TAG, "******* File not found. Did you" +
        // " add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static void copyAsset(Context context, AssetManager am, boolean force) {
    File newDir = getSDDir(context);
    File sd = Environment.getExternalStorageDirectory();
    if (sd != null && sd.exists() && sd.isDirectory() && newDir.exists() && newDir.isDirectory()) {
        File hpDir = new File(sd, ".hp48");
        if (hpDir.exists()) {
            File allFiles[] = hpDir.listFiles();
            if (allFiles != null && allFiles.length > 0) {
                Log.i("x48",
                        "Moving x48 files from the old dir " + sd.getAbsolutePath() + " to the proper one :");
                for (File file : allFiles) {
                    File newFile = new File(newDir, file.getName());
                    Log.i("x48", "Moving " + file.getAbsolutePath() + " to " + newFile);
                    file.renameTo(newFile);
                }/*from w ww  .  j  a  va  2  s .com*/
            }
            Log.i("x48", "Deleting old directory");
            hpDir.delete();
        }
    }
    copyAsset(am, newDir, force);
}

From source file:Main.java

public static void appendLog(String text) {
    Log.d("LOGFILE", text);
    SimpleDateFormat sTime = new SimpleDateFormat("dd/MMM/yyyy - hh:mm:ss");
    String timeFormat = sTime.format(new Date());

    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File(sdCard.getAbsolutePath() + "/Cura/Logs/");
    dir.mkdirs();/* ww  w  .j  ava2s .  co m*/
    File logFile = new File(dir, "Cura_Logs_DEBUG.txt");
    if (!logFile.exists()) {
        try {
            logFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
        if (text.compareTo("wipe") == 0)
            logFile.delete();
        else {
            buf.append("[" + timeFormat + "] - ");
            buf.append(text);
            buf.newLine();
            buf.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}