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

/**
 * DB test//from  w w w  . j  a  va 2s.co  m
 */
public static void runBackup(Context context) {
    File file = context.getDatabasePath("PhotoDeskHiddenFolder.db");
    int size = (int) file.length();

    String path = Environment.getExternalStorageDirectory() + "/PhotoDesk/";
    try {
        byte[] buffer = new byte[size];
        InputStream inputStream = new FileInputStream(file);
        inputStream.read(buffer);
        inputStream.close();

        File outputDBDirectory = new File(path);
        if (!outputDBDirectory.isDirectory())
            outputDBDirectory.mkdir();

        path += "test.db";

        File outputFile = new File(path);
        FileOutputStream outputStream = new FileOutputStream(outputFile);
        outputStream.write(buffer);
        outputStream.flush();
        outputStream.close();

    } catch (Exception e) {
    }
}

From source file:Main.java

public static void restoreDb(File currentDB, String restoreDBFileName) {
    try {/*from w  w  w .  java  2 s. c  om*/
        backupDb(currentDB);
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            File f = new File(sd.getAbsolutePath() + "/AccountManagement");
            if (!f.exists()) {
                f.mkdir();
            }
            String restoreDBPath = "AccountManagement/" + restoreDBFileName;
            File restoreDB = new File(sd, restoreDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(restoreDB).getChannel();
                FileChannel dst = new FileOutputStream(currentDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:Main.java

public static String getAbsolutePathFromNoStandardUri(Uri mUri) {
    String filePath = null;/*from   w  ww.ja  v  a  2s .  co  m*/

    String mUriString = mUri.toString();
    mUriString = Uri.decode(mUriString);

    String pre1 = "file://" + SDCARD + File.separator;
    String pre2 = "file://" + SDCARD_MNT + File.separator;

    if (mUriString.startsWith(pre1)) {
        filePath = Environment.getExternalStorageDirectory().getPath() + File.separator
                + mUriString.substring(pre1.length());
    } else if (mUriString.startsWith(pre2)) {
        filePath = Environment.getExternalStorageDirectory().getPath() + File.separator
                + mUriString.substring(pre2.length());
    }
    return filePath;
}

From source file:Main.java

/**
 * Return the absolute path to the hub database.
 *
 * @return path The path to the db as a string
 *//*from   w w w.  j  a  v  a 2 s  .  com*/
private static String getDbPath() {
    if (Debug)
        Log.i(TAG, "getDBPath() called.");

    String path = Environment.getExternalStorageDirectory().getPath() + "/" + BASE_DIR;

    File dbDir = new File(path);
    if (!dbDir.isDirectory()) {
        try {
            if (Debug)
                Log.i(TAG, "Trying to create " + path);
            dbDir.mkdirs();
        } catch (Exception e) {
            final Writer result = new StringWriter();
            final PrintWriter printWriter = new PrintWriter(result);
            e.printStackTrace(printWriter);
            Log.e(TAG, result.toString());
        }
    }
    return path;
}

From source file:Main.java

public static void notifyFileSystemChanged(String path, Context mContext) {
    if (path == null)
        return;//from ww  w .j a v  a2 s  .  co  m
    final File f = new File(path);
    final Intent intent;
    if (f.isDirectory()) {
        intent = new Intent(Intent.ACTION_MEDIA_MOUNTED);
        intent.setClassName("com.android.providers.media", "com.android.providers.media.MediaScannerReceiver");
        intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));

    } else {
        intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(new File(path)));

    }
    mContext.sendBroadcast(intent);
}

From source file:Main.java

/**
 * /*from ww w .  j  av  a 2 s.c o m*/
 * @param subDirName
 * @return
 */
public static String[] constructFileList(String subDirName) {
    List<String> fileList = null;

    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        File subDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
                + DIR_NAME + File.separator + subDirName);

        if (subDir.exists()) {
            File[] files = subDir.listFiles();

            for (int i = 0; i < files.length; i++) {
                if (fileList == null) {
                    fileList = new ArrayList<String>();
                }

                // remove the extension
                fileList.add(files[i].getName().split("\\.")[0]);
            }
        } else {
            Log.e(LOG_TAG, "Nothing to import");
        }
    }

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

From source file:Main.java

public static String getSdCacheDir(Context context) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

        if (true) {
            Context appContext = context.getApplicationContext();

            File amap = appContext.getExternalFilesDir("offlineMap");
            return amap.getAbsolutePath() + "/";
        } else {//from  w  ww  .  j  a  va2s  . co  m

            File fExternalStorageDirectory = Environment.getExternalStorageDirectory();
            File autonaviDir = new File(fExternalStorageDirectory, "amapsdk");
            boolean result = false;
            if (!autonaviDir.exists()) {
                result = autonaviDir.mkdir();
            }
            File minimapDir = new File(autonaviDir, "offlineMap");
            if (!minimapDir.exists()) {
                result = minimapDir.mkdir();
            }
            return minimapDir.toString() + "/";
        }

    } else {
        return "";
    }
}

From source file:Main.java

/**
 * Get the external app cache directory.
 *
 * @param context/*w  w  w  . j ava 2 s .  c o  m*/
 *            The context to use
 * @return The external cache dir
 */
private static File getExternalCacheDir(Context context) {
    // if (BaseVersionUtils.hasFroyo()) {
    // return context.getExternalCacheDir();
    // }

    // Before Froyo we need to construct the external cache dir ourselves
    final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";

    return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
}

From source file:Main.java

/**
 * Get the external storage path of the device
 *
 * @return The external storage path of the device.
 *//*from ww  w .  jav  a2  s.  c om*/
public static String getStorageRootDirectory() {
    try {
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return null;
        }
    } catch (Exception e) {
        // Catch exception is trying to fix a crash inside of Environment.getExternalStorageState().
        e.printStackTrace();
        return null;
    }
    String rootDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
    File file = new File(rootDir);
    if (!file.exists()) {
        if (!file.mkdirs()) {
            return null;
        }
    }
    return rootDir;
}

From source file:Main.java

/**
 * Get the external app cache directory.
 * //from   ww w  .  j  a v  a2 s  .  com
 * @param context
 *            The context to use
 * @return The external cache dir
 */
public static File getExternalCacheDir(Context context) {
    if (hasExternalCacheDir()) {
        return context.getExternalCacheDir();
    }

    // Before Froyo we need to construct the external cache dir ourselves
    final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
    return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
}