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 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 {//  ww  w  . java  2  s.c  o m
            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;

}

From source file:Main.java

public static boolean copyAsset(Context context, AssetManager am, boolean force) {
    File newDir = getSDDir(context);
    File sd = Environment.getExternalStorageDirectory();
    if (sd != null && sd.exists() && sd.isDirectory() && newDir != null && 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 ww w .  j  av a 2 s  . c o  m*/
            }
            Log.i("x48", "Deleting old directory");
            hpDir.delete();
        }
    }
    return copyAsset(am, newDir, force);
}

From source file:Main.java

public static int[] readBin83PtIndex(String dir, String fileName) {
    int i = 0;/*from w w  w .ja v  a  2  s  .  c  o  m*/
    int x = 0;
    int[] tab = new int[83];
    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        x = in.readInt();
        while (true) {
            tab[i] = x;
            i++;
            x = in.readInt();
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return tab;
}

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 w w  w.ja v a 2s.  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 int[] readBinIntArray(String dir, String fileName, int size) {
    int x;//from   www  . jav a 2s.com
    int i = 0;
    int[] tab = new int[size];
    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        x = in.readInt();
        while (true) {
            tab[i] = x;
            i++;
            x = in.readInt();
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return tab;
}

From source file:Main.java

public static float[] readBinFloat(String dir, String fileName, int size) {
    float x;/* w  w  w. j  av  a2s.  c om*/
    int i = 0;
    float[] tab = new float[size];
    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        x = in.readFloat();
        while (i < size) {
            tab[i] = x;
            i++;
            x = in.readFloat();
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return tab;
}

From source file:Main.java

/***
 * write a line to the log file/*from   www. j  a  v a 2s.c o m*/
 * 
 * @param line
 */
public static void writeToLog(String line) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date();
    File f = new File(Environment.getExternalStorageDirectory() + File.separator + folderName);
    boolean success = true;
    if (!f.exists()) {
        success = f.mkdir();
    }
    if (success) {
        // Do something on success
        File fileDir = new File(f, dateFormat.format(date) + ".txt");
        try {
            FileOutputStream os = new FileOutputStream(fileDir, true);
            os.write(line.getBytes());
            os.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static float[] readBinTextureArray(String dir, String fileName, int size) {
    float x;/*from w  w  w .ja v  a2 s  .  c om*/
    int i = 0;
    float[] tab = new float[size];
    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        x = in.readFloat(); // convert here for OpenGl
        while (true) {
            tab[i] = x / 255.0f;
            i++;
            x = in.readFloat(); // convert here for OpenGl
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return tab;
}

From source file:Main.java

public static String[] getExternalDirs(Context context) {
    List<String> paths = new ArrayList<String>();
    String[] secondaryDirs = getSecondaryDirs();
    if (secondaryDirs != null) {
        for (String secondaryDir : secondaryDirs) {
            paths.add(secondaryDir + "/roadsigns");
        }//from  w  w w. ja  v a  2s  . co m
    }

    File externalStorageDir = Environment.getExternalStorageDirectory();
    if (externalStorageDir != null && externalStorageDir.isDirectory()) {
        paths.add(Environment.getExternalStorageDirectory().getPath() + "/roadsigns");
    }

    return paths.toArray(new String[paths.size()]);
}

From source file:Main.java

public static String saveImg(Bitmap b, String name) throws Exception {
    String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "QuCai/shareImg/";
    File mediaFile = new File(path + File.separator + name + ".jpg");
    if (mediaFile.exists()) {
        mediaFile.delete();//  w w  w  .  j  av a  2  s  .  com

    }
    if (!new File(path).exists()) {
        new File(path).mkdirs();
    }
    mediaFile.createNewFile();
    FileOutputStream fos = new FileOutputStream(mediaFile);
    b.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();
    b.recycle();
    b = null;
    System.gc();
    return mediaFile.getPath();
}