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 GetSdcardSystemPath() {
    String rootpath = "";
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        rootpath = Environment.getExternalStorageDirectory().toString();
    }/*  w  w w .  j a v  a  2  s.  co m*/
    return rootpath;
}

From source file:Main.java

@SuppressLint("NewApi")
public static void getURL(String path) {
    String fileName = "";
    String dir = "/IndoorNavi/";
    File sdRoot = Environment.getExternalStorageDirectory();
    try {//  w  ww.j a v  a2  s.  c o m
        // Open the URLConnection for reading
        URL u = new URL(path);
        // URL u = new URL("http://www.baidu.com/");
        HttpURLConnection uc = (HttpURLConnection) u.openConnection();

        int code = uc.getResponseCode();
        String response = uc.getResponseMessage();
        //System.out.println("HTTP/1.x " + code + " " + response);
        for (int j = 1;; j++) {
            String key = uc.getHeaderFieldKey(j);
            String header = uc.getHeaderField(j);
            if (!(key == null)) {
                if (key.equals("Content-Name"))
                    fileName = header;
            }
            if (header == null || key == null)
                break;
            //System.out.println(uc.getHeaderFieldKey(j) + ": " + header);
        }
        Log.i("zhr", fileName);
        //System.out.println();

        try (InputStream in = new BufferedInputStream(uc.getInputStream())) {

            // chain the InputStream to a Reader
            Reader r = new InputStreamReader(in);
            int c;
            File mapFile = new File(sdRoot, dir + fileName);
            mapFile.createNewFile();
            FileOutputStream filecon = new FileOutputStream(mapFile);
            while ((c = r.read()) != -1) {
                //System.out.print((char) c);
                filecon.write(c);
                filecon.flush();

            }
            filecon.close();
        }

    } catch (MalformedURLException ex) {
        System.err.println(path + " is not a parseable URL");
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

From source file:Main.java

/**
 * Dump the database file to external storage
 *
 * @param packageName/*from   w  ww  .  j  av  a2 s . com*/
 * @param fileName
 * @throws IOException
 */
public static void dumpDatabase(String packageName, String fileName) throws IOException {
    File dbFile = new File("/data/data/" + packageName + "/databases/" + fileName);
    if (dbFile.exists()) {
        FileInputStream fis = new FileInputStream(dbFile);
        String outFileName = Environment.getExternalStorageDirectory() + "/" + fileName;
        OutputStream output = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = fis.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
        output.flush();
        output.close();
        fis.close();
    }
}

From source file:Main.java

/**
 * Reads in model 83 points and returns a double array float containing the
 * coordinates x & y.// w  w w . j  a  va2 s .c o m
 */
public static float[][][] readBinSFSV(String dir, String fileName) {
    float[][][] array3D = new float[60][83][3];
    float x;
    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 {
        for (int k = 0; k < 3; k++) {
            for (int j = 0; j < 83; j++) {
                for (int i = 0; i < 60; i++) {
                    x = in.readFloat();
                    array3D[i][j][k] = x;
                }
            }
        }
    } 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 array3D;
}

From source file:Main.java

public static String getSdcardFileName(String fileName) {
    return Environment.getExternalStorageDirectory() + FILE_DIR + "/" + fileName;
}

From source file:Main.java

public static boolean installRingtone(final Context context, int resid, final String toneName) {

    String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
    String filename = toneName + ".mp3";
    File fileAlarms = new File(exStoragePath, "/Notifications");
    final File fileTone = new File(fileAlarms, filename);

    if (fileTone.exists())
        return false;

    boolean exists = fileAlarms.exists();
    if (!exists) {
        fileAlarms.mkdirs();/*from w  w  w . j  a va  2  s.c o m*/
    }

    if (fileTone.exists())
        return false;

    byte[] buffer = null;
    InputStream fIn = context.getResources().openRawResource(resid);
    int size = 0;

    try {
        size = fIn.available();
        buffer = new byte[size];
        fIn.read(buffer);
        fIn.close();
    } catch (IOException e) {
        return false;
    }

    FileOutputStream save;
    try {
        save = new FileOutputStream(fileTone);
        save.write(buffer);
        save.flush();
        save.close();
    } catch (FileNotFoundException e) {
        return false;
    } catch (IOException e) {
        return false;
    }

    MediaScannerConnection.scanFile(context, new String[] { fileTone.getAbsolutePath() }, null,
            new MediaScannerConnection.OnScanCompletedListener() {

                @Override
                public void onScanCompleted(String path, Uri uriTone) {

                    ContentValues values = new ContentValues();
                    values.put(MediaStore.MediaColumns.DATA, fileTone.getAbsolutePath());
                    values.put(MediaStore.MediaColumns.TITLE, toneName);
                    values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mp3");
                    values.put(MediaStore.Audio.Media.ARTIST, "zom");

                    //new
                    values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
                    values.put(MediaStore.Audio.Media.IS_ALARM, true);
                    values.put(MediaStore.Audio.Media.IS_MUSIC, false);

                    // Insert it into the database
                    Uri newUri = context.getContentResolver().insert(uriTone, values);

                    //                RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);

                    //   Settings.System.putString(context.getContentResolver(),
                    //         Settings.System.RINGTONE, uri.toString());

                }
            });

    return true;
}

From source file:Main.java

public static File createFile(Activity activity, String name, boolean isThumbnail) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    String subDir;//from w ww .j  a  v  a 2 s  .co m
    String fileType;
    if (isThumbnail) {
        subDir = "/thumbnails";
        fileType = "thumbnail_";
    } else {
        subDir = "/backgrounds";
        fileType = "background_";
    }
    File storageDir = new File(Environment.getExternalStorageDirectory() + "/Android/data/"
            + activity.getApplicationContext().getPackageName() + subDir);
    // Create the storage directory if it does not exist
    if (!storageDir.exists()) {
        if (!storageDir.mkdirs()) {
            return null;
        }
    }
    File thumbnailFile;
    String thumbnailName = fileType + name;
    thumbnailFile = new File(storageDir.getPath() + File.separator + thumbnailName);
    return thumbnailFile;
}

From source file:Main.java

public static String zip(String filename) throws IOException {
    BufferedInputStream origin = null;
    Integer BUFFER_SIZE = 20480;/*from   w ww. j ava 2 s  . com*/

    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() + "/" + "flockZip.zip";
        ZipOutputStream out = new ZipOutputStream(
                new BufferedOutputStream(new FileOutputStream(compressedFile)));
        out.setLevel(9);
        try {
            byte data[] = new byte[BUFFER_SIZE];
            FileInputStream fi = new FileInputStream(uncompressedFile);
            System.out.println("Filename: " + uncompressedFile);
            System.out.println("Zipfile: " + compressedFile);
            origin = new BufferedInputStream(fi, BUFFER_SIZE);
            try {
                ZipEntry entry = new ZipEntry(
                        uncompressedFile.substring(uncompressedFile.lastIndexOf("/") + 1));
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
                    out.write(data, 0, count);
                }
            } finally {
                origin.close();
            }
        } finally {
            out.close();
        }
        return "flockZip.zip";
    }
    return null;
}

From source file:Main.java

public static File getImageSavePath() {
    final File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
            + "MGJ-IM" + File.separator + "images");
    if (!folder.exists()) {
        folder.mkdirs();//from  w  ww .ja va2  s . c  om
    }
    return folder;
}

From source file:Main.java

private static File getStorageDir(Context context) {
    if (storageDir == null) {
        File file = Environment.getExternalStorageDirectory();
        if (file.exists()) {
            return file;
        }// w  w  w .  ja v a  2  s. c  o  m

        storageDir = context.getFilesDir();
    }

    return storageDir;
}