How can I copy database file into sdcard if only it exists? Here is my code:
public boolean checkdatabase() {
boolean checkdb = false;
try {
File databaseFile = myContext.getDatabasePath("database.db");
checkdb = databaseFile.exists();
} catch(SQLiteException e) {
System.out.println("Database doesn't exist");
}
return checkdb;
}
public void opendatabase() {
boolean dbexist = checkdatabase();
if (dbexist) {
myDatabase = SQLiteDatabase.openDatabase("data/data/***/databases/database.db", null, SQLiteDatabase.OPEN_READWRITE);
} else {
createdatabase();
System.out.println("Database doesn't exist");
myDatabase = SQLiteDatabase.openDatabase("data/data/***/databases/database.db", null, SQLiteDatabase.OPEN_READWRITE);
}
}
private copydatabase() throws IOException {
String DB_PATH = "data/data/***/databases/";
File f = new File(DB_PATH);
if (!f.exists()) {
f.mkdir();
}
String DATABASE_NAME = "database.db";
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
String outFileName = DB_PATH + DATABASE_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
This code copies databse into internal memory. I want to check if external memory (sdcard) exists, and if it is true - copy database file into it and then access it everywhere in my app.
try this :
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//{package name}//databases//{database name}";
String backupDBPath = "{database name}";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel bst = new FileOutputStream(backupDB).getChannel();
bst.transferFrom(src, 0, src.size());
src.close();
bst.close();
}
}
} catch (Exception e) {
}
Don't forget to include this in your manifest file :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Something like this should work:
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)){
File root = Environment.getExternalStorageDirectory();
String DATABASE_NAME = "database.db";
File destination = new File(root, "nameofthefile");
try{
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
FileOutputStream myOutput = new FileOutputStream(destination);
//Handle writing
}catch(Exception e){ //Change it for appropiate catches
// Handle exceptions
}finally{
myOutput.flush();
myOutput.close();
myInput.close();
}
}else{
// No SD card or only reading permissions
}
Remember that you need to declare this in your AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.