How do I backup sqlite db files using MediaStore on Android? - android

I have backed up the sqlite db file to storage using the code below
But from Android 11, I have to use mediastore, but I don't know how.
Can I back up sqlite db using mediastore?
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//" + packageName + "//databases//" + dbName + ".db";
String backupDBPath = "//" + saveFolder + "//" + dbName + ".db"; // 백업 위치
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
Log.i("sinwhod","currentDB = " + currentDB);
Log.i("sinwhod","backupDB = " + backupDB);
Log.i("sinwhod","backupDBPath = " + 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) {
}

Related

Can you replace android database?

I'm new to android studio and I'm trying to do a backup of my database from my app. I can pull the database.db from the data/data/databases, but when I cleared the data and try to import the copy of the db to data/data/databases nothing happen.
The question is can you not modify data/data/databases?
here is the Code for Backup And Restore the Database
private void importDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//" + "<package name>"
+ "//databases//" + "<database name>";
String backupDBPath = "<backup db filename>";
File backupDB = new File(data, currentDBPath);
File currentDB = new File(sd, backupDBPath);
FileChannel src = new FileInputStream(backupDB).getChannel();
FileChannel dst = new FileOutputStream(currentDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getApplicationContext(), "Import Successful!",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Import Failed", Toast.LENGTH_SHORT)
.show();
}
}
private void exportDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//" + "<package name>"
+ "//databases//" + "<db name>";
String backupDBPath = "<destination>";
File currentDB = new File(data, currentDBPath);
File backupDB = 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();
Toast.makeText(getApplicationContext(), "Backup Successful!",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Backup Failed", Toast.LENGTH_SHORT)
.show();
}
}

Android studio copying DB file to internal storage not wroking

I tried to retype see whether any typing error there but it still not working.
It does not even prompt me that I have any errors
I am thinking that is it my internal storage path file given wrongly?
This is where the internal storage file that i wanted to copy to!
This is how i called my method at onclick function:
else if (view == findViewById(R.id.btnBackup)){
exportDatabase("stock.db");
}
}
This is the method for exporting database:
public void exportDatabase(String databaseName) {
try {
File sd = getFilesDir();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//"+getPackageName()+"//databases//"+databaseName+"";
String backupDBPath = "//MyDb";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
Toast.makeText(this, "Backup Done!", Toast.LENGTH_SHORT);
}
} catch (Exception e) {
Toast.makeText(this, "Fail!", Toast.LENGTH_SHORT);
}
}
Use this method work perfect.
public void exportDB(Context mContext) {
try {
File sd = mContext.getExternalFilesDir("/database/");
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);
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) {
}
}

duplicate default Database into temp Database

I would like to create a temporary database, this is the code:
String PATH = "/data/data/" + appContext.getPackageName() + "/databases/";
List<File> files = getListFiles(new File(PATH));
//File dbFile = appContext.getDatabasePath(PreferenceConstants.TEMP_DB_DATABASE_STORE);
File dbFile = new File(PATH, PreferenceConstants.TEMP_DB_DATABASE_STORE);
FileInputStream is;
if (!dbFile.exists()) {
try {
dbFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}else
dbFile.delete();
try {
is = new FileInputStream(appContext.getDatabasePath(PreferenceConstants.DB_DATABASE_STORE));
FileUtils.copyInputStreamToFile(is, dbFile);
} catch (IOException e) {
e.printStackTrace();
}
but no file are created into database folder, why?
after copy I would like to open another database , copy the tables and append the table in the temp database created.
EDIT
very simple method:
DatabaseHelper dbIng = new DatabaseHelper(appContextDialog, "temp_database.db");
dbIng.closeDB();
This is what I use for importing and exporting databases:
Dont forget about the permissions.
public void exportDatabase(){
try
{
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
String currentDBPath = "//data//MY.PACKAGE.NAME//databases//MY_DATABASE_NAME";
String backupDBPath = "MY_DATABASE_FILE.db";
File currentDB = new File(data, currentDBPath);
File backupDB = 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();
Toast.makeText(c, c.getResources().getString(R.string.exporterenToast), Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(c, c.getResources().getString(R.string.portError), Toast.LENGTH_SHORT).show();
Log.d("Main", e.toString());
}
}
public void importDatabase(){
try
{
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
String currentDBPath = "//data//" + "MY.PACKAGE.NAME" + "//databases//" + "MY_DATABASE_NAME";
String backupDBPath = "MY_DATABASE_FILE.db";
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();
Toast.makeText(c, c.getResources().getString(R.string.importerenToast), Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(c, c.getResources().getString(R.string.portError), Toast.LENGTH_SHORT).show();
}
}

Android Backup Database no results

I am trying to backup my database in my android app using sqlite. As this is the given code in google, but its not showing any result even error in logcat. What it is supposed to mean? Sorry im just new to android dev..
try {
File sd = Environment.getDataDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
if (sd.canWrite()) {
String currentDBPath = "//data//" + "com.example.jes.myapplication"
+ "//databases//" + "TINDERO.DB";
String backupDBPath = "//data//" + "com.example.jes.myapplication"
+ "//databases//" + "//TINDERO_BACKUP//" + "TINDERO.DB";
File currentDB = new File(data, currentDBPath);
File backupDB = 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();
Toast.makeText(getApplicationContext(), "Backup Successful!",
Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Failed",Toast.LENGTH_SHORT).show();
}
}
});
your if (sd.canWrite()) might be always false. Put a break point inside the block and see if it is entering the if block. Or put an else block and check.

Export database in android

I am exporting my sqllite database using this code.
public void exportDB() {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source = null;
FileChannel destination = null;
String backupDBPath = "contactsManager.db";
String currentDBPath = "/data/" + "com.packagename"
+ "/databases/" + backupDBPath;
System.out.println("oooooooooooooo" + currentDBPath);
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
Toast.makeText(getApplicationContext(), "DB Exported!", Toast.LENGTH_LONG)
.show();
System.out.println("---------db exporeted");
} catch (Exception e) {
e.printStackTrace();
}
}
But I am getting a file not found exception.This is the log.
java.io.FileNotFoundException: /data/data/packagename/databases/contactsManager.db: open failed: ENOENT (No such file or directory)
The currentDB (source) does not exist. Check your path.
Also make sure the destinationpath exists with backupDB.mkdir()

Categories

Resources