Android studio copying DB file to internal storage not wroking - android

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) {
}
}

Related

How to restore Room database that i have exported?

In my App i need to implement restoring room database that i have exported to local storage. i have managed to copy all 03 database files (.db -shm and wal) using get getDatabasePath(DATABASE_NAME).getAbsolutePath(). For replacing it back, i am copying these exported files back to original database path, which works fine, but no data is showing when i access it from inside the app. The exported database is clearly showing all the table with DB Browser tool. I am not sure why its not working.This is a backup button, which generates backup files. I am not sure if i am approaching it wrong or there is something wrong in my code.
backpathbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String currentDBPath =
getDatabasePath(DATABASE_NAME).getAbsolutePath();
String backupDBPath = "my_db";
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
String rot = Environment.getExternalStorageDirectory().toString();
File currentDB = new File(currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
try {
db.close();
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
backupDB = new File(sd, backupDBPath+"-shm");
src = new FileInputStream(currentDB+"-shm").getChannel();
dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
backupDB = new File(sd, backupDBPath+"-wal");
src = new FileInputStream(currentDB+"-wal").getChannel();
dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();}
catch (IOException e) {
e.printStackTrace();
}}} });
Here is the restore button
restorebutton.setOnClickListener(v -> {
db.close();
String back=
getDatabasePath(DATABASE_NAME).getAbsolutePath();
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
String current = "my_db";
String currentsh = "my_db-shm";
String currentwl = "my_db-wal";
String filepath= sd+"/"+current;
String shmfile= sd+"/"+currentsh;
String currenwl= sd+"/"+currentwl;
File dest=new File(back);
Log.d(TAG, "Following files are in absolute path " + dest.list()); // returning null
try {
copyFile(filepath,dest);
copyFile(shmfile,dest);
copyFile(currenwl,dest);
Log.d(TAG, "Copied Sucessfully "); // it Copies without error
}
catch (IOException e) {
e.printStackTrace();
}
});
}
Here is my copy file function
public void copyFile(String src, File dst) throws IOException
{
FileChannel inChannel = new FileInputStream(String.valueOf(src)).getChannel();
FileChannel outChannel = new FileOutputStream(dst).getChannel();
try
{
inChannel.transferTo(0, inChannel.size(), outChannel);
}
finally
{
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
After returning back to my MainActivity, restored data is not showing, even after closing and reopening the app again.

After backing up a database, I can't restore the selected database

I'm having trouble retrieving the .db file from DB card.
I used this code for backup database:
private void DBBackup() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = getContext().getDatabasePath(DBHelper.DB_NAME).getPath();
String backupDBPath = "DBBackupTest";
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(getContext(), "Export Successful!", Toast.LENGTH_SHORT).show();
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
after several minutes, I want to import the backed up database. This is the code for Import
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "/storage/emulated/legacy/DBBackup";
String backupDBPath = "DBBackupTest"; // From SD directory.
File backupDB = new File(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(getContext(), "Import Successful!", Toast.LENGTH_SHORT).show();
}
} catch (Throwable e) {
Toast.makeText(getContext(), "Import Failed!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
The Toast show "Import Successful!" but when i check the database by using stetho. the database isn't attached. I need your help on this. thanks!
I fixed the problem by changing my currentDB and change it from:
File currentDB = new File(sd, backupDBPath);
to:
File currentDB = new File(getContext().getDatabasePath(DBHelper.DB_NAME).getPath());

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();
}
}

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();
}
}

trying to export db to SDCARD

I'm pretty new to android and I'm trying to export my DB to the SDCARD so I can import it into my other application.
Any suggestions?
Thanx in advance.
Export example:
public void exportDB(){
try {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
String currentDBPath = "data/data/com.mypack.myapp/databases/mydb.db";
String backupDBPath = sd + "/filename.db";
File currentDB = new File(currentDBPath);
File backupDB = new File(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();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Import is very similar to export:
public void importDB(){
try {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
String currentDBPath = sd + "/filename.db";
String backupDBPath = "data/data/com.mypack.myapp/databases/mydb_2.db";
File currentDB = new File(currentDBPath);
File backupDB = new File(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();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Although this might not be the best solution for what you are trying to do, your database is already stored in /data/data/your.package.name. You just need to load it as a file and save to SD card. Check here for the the code that does this:
http://www.screaming-penguin.com/node/7749

Categories

Resources