Example usage for android.os Environment getDataDirectory

List of usage examples for android.os Environment getDataDirectory

Introduction

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

Prototype

public static File getDataDirectory() 

Source Link

Document

Return the user data directory.

Usage

From source file:Main.java

/**
 * Calculates the free memory of the device. This is based on an inspection of the filesystem, which in android
 * devices is stored in RAM./*  w w  w  .j  ava2 s .c o m*/
 *
 * @return Number of bytes available.
 */
public static long getAvailableInternalMemorySize() {
    final File path = Environment.getDataDirectory();
    final StatFs stat = new StatFs(path.getPath());
    final long blockSize;
    final long availableBlocks;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        blockSize = stat.getBlockSizeLong();
        availableBlocks = stat.getAvailableBlocksLong();
    } else {
        //noinspection deprecation
        blockSize = stat.getBlockSize();
        //noinspection deprecation
        availableBlocks = stat.getAvailableBlocks();
    }
    return availableBlocks * blockSize;
}

From source file:Main.java

public static boolean importDatabase(String dbName) {

    try {/*from  ww  w.j a  va 2  s  .  com*/
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + PACKAGENAME + "//databases//" + dbName;
            String backupDBPath = "/AttendanceManager/" + dbName;
            File backupDB = new File(data, currentDBPath);
            File currentDB = new File(sd, backupDBPath);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
        }
    } catch (Exception e) {
        return false;
    }
    return true;
}

From source file:Main.java

public static File getDataRoot() {
    try {//from  w w w . ja  va2s  .  co  m
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
                || (Environment.getExternalStorageDirectory().exists()
                        && Environment.getExternalStorageDirectory().canWrite())) {
            return Environment.getExternalStorageDirectory();
        } else {
            return Environment.getDataDirectory();
        }
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static void restoreDb(File currentDB, String restoreDBFileName) {
    try {//from w w  w.  j  ava 2s .  com
        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

/**
 * Create a backup of the database./*from  ww  w  . j  a  v a2 s  .  co m*/
 *
 * @param context who calls the function.
 * @param dbName  Name of the database to backup. (If empty, radiomap.db is used).
 */
public static void exportDb(Context context, String dbName) {
    try {
        if (dbName.equals(""))
            dbName = "radiomap.db";

        File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + context.getApplicationContext().getPackageName()
                    + "//databases//" + dbName;
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, dbName);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();

            Toast.makeText(context, "Datenbank nach Downloads exportiert!", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Log.e("Utils", e.getMessage());
        Toast.makeText(context, "Exportieren fehlgeschlagen!", Toast.LENGTH_LONG).show();
    }
}

From source file:Main.java

/**
 * Create a backup of the database.//w  w w . j a  v a  2s. co  m
 *
 * @param context who calls the function.
 * @param dbName  Name of the database to backup. (If empty, radiomap.db is used).
 */
public static void importDb(Context context, String dbName) {
    try {
        if (dbName.equals(""))
            dbName = "radiomap.db";

        File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + context.getApplicationContext().getPackageName()
                    + "//databases//" + dbName;
            File backupDB = new File(data, currentDBPath);
            File currentDB = new File(sd, dbName);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();

            Toast.makeText(context, "Datenbank von Downloads importiert!", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Log.e("Utils", e.getMessage());
        Toast.makeText(context, "Import fehlgeschlagen!", Toast.LENGTH_LONG).show();
    }
}

From source file:Main.java

public static long getAvailableInnerSpace() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    long realSize = blockSize * availableBlocks;
    return realSize;
}

From source file:Main.java

/**
 * /* w w  w . j  a  va2  s.co  m*/
 * @param context
 * @param backupFolder
 * @param db_name
 * @return
 */
public static boolean backupDB(Context context, String backupFolder, String db_name) {
    boolean result = false;

    try {
        String current_date = DateToString(GetToday(), "dd-MM-yyyy");

        File data = Environment.getDataDirectory();
        File sdcard = new File(Environment.getExternalStorageDirectory(), backupFolder + "/");
        sdcard.mkdirs();

        if (sdcard.canWrite()) {
            String currentDBPath = "//data//" + context.getPackageName() + "//databases//" + db_name + "";
            String backupDBPath = "backup_" + db_name + "_" + current_date + ".db";

            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sdcard, backupDBPath);

            if (currentDB.exists()) {
                InputStream input = new FileInputStream(currentDB);
                OutputStream output = new FileOutputStream(backupDB);

                byte[] buffer = new byte[1024];
                int length;

                while ((length = input.read(buffer)) > 0) {
                    output.write(buffer, 0, length);
                }

                output.flush();
                output.close();
                input.close();

                result = true;
            }
        }
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }

    return result;
}

From source file:Main.java

/**
 * @return Total internal memory//  www. ja v  a 2  s  . c om
 */
public static int getTotalInternalMemorySize() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    int blockSize = stat.getBlockSize();
    int totalBlocks = stat.getBlockCount();
    return totalBlocks * blockSize;
}

From source file:Main.java

/**
 * @return Available internal memory/*from  w ww .j a  va 2s  .  c om*/
 */
public static int getAvailableInternalMemorySize() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    int blockSize = stat.getBlockSize();
    int availableBlocks = stat.getAvailableBlocks();
    return availableBlocks * blockSize;
}