Swisyn
11/30/2016 - 8:48 PM

Database Operations

Database Operations

   public static void CopyDBIfNotExists(Context context, String dbName) {
        File db = new File(context.getDatabasePath(dbName).getPath());
        if (!db.exists()) {
            File dbDirectory = new File("/data/data/" + context.getPackageName() + "/databases/");
            dbDirectory.mkdirs();
            try {
                CopyDB(context.getAssets().open(dbName), new FileOutputStream(context.getDatabasePath(dbName).getPath()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void CopyDB(InputStream inputStream, OutputStream outputStream) {
        byte[] buffer = new byte[1024];
        int length;
        try {
            while ((length = inputStream.read(buffer)) > 0) outputStream.write(buffer, 0, length);
            outputStream.flush();
            inputStream.close();
            outputStream.close();
            Log.d("CopyDB", "> Database file has been copied successfully!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }