my problem is the following:
I have correctly moved the SQlite database on the local SD, but the file is not visible both from the device and the PC via USB connection.
The app does not raise any error.
Here is the method that performs the copy:
private void exportDB(){
DBHelper db = DBHelper.getInstance(this);
String DBName = db.getDBName();
File dbPath = this.getDatabasePath(DBName);
String DBPath = dbPath.getAbsolutePath();
//Toast.makeText(this, "DB Path : " + DBPath , Toast.LENGTH_LONG).show();
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
String currentDBPath = DBPath;
String backupDBPath = "Download/"+DBName;
File currentDB = new File(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(this, "DB Exported!", Toast.LENGTH_LONG).show();
} catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
for transferting DB, I think it may achievable in rooted device.
Related
I am trying to restore a database from the phone storage in order to use it in my application.
I successfully did a backup for my database to the internal storage but when I am trying to restore it the following io.exception pops up
java.io.IOException: open failed: ENOENT (No such file or directory)
How can I solve this problem?
However, I tried the follosing existing solutions but they didn't work
Android - java.io.FileNotFoundException,
is it possible backup and RESTORE a database file in android? non root devices,
Restoring SQLite DB file
private void restoreDatabase(Context context) {
String packagename = getPackageName();
File sdcard = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
try {
if (sdcard.canWrite()) {
String currentDBPath = "//data/"+getPackageName() +"/databases/" + DATABASE_NAME;
String backupDBPath = DATABASE_NAME;
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sdcard, backupDBPath);
currentDB.createNewFile();
if (currentDB.exists()) {
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(), "Database Restored successfully", Toast.LENGTH_SHORT).show();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
}
}
Please don't mark this question as duplicate
after exporting your database you can use this function to import the database, I had it testet and it works fine :
private void importDB() {
String appDataPath = getApplicationContext().getApplicationInfo().dataDir;
File dbFolder = new File(appDataPath + "/databases");//Make sure the /databases folder exists
dbFolder.mkdir();//This can be called multiple times.
//EDITED
File dbFilePath = new File(appDataPath + "/databases/"+"yourDataBaseName");
try {
//EDITED
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "YourBackupFolder","yourDataBaseFileName);
FileInputStream inputStream = new FileInputStream(file); //use your database name
OutputStream outputStream = new FileOutputStream(dbFilePath);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer))>0)
{
outputStream.write(buffer, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (IOException e){
//handle
e.printStackTrace();
}
}
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) {
}
}
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()
I stored some data into SQLite database, and now i have to get those data from device's internal storage any how, i am using below code but nothing happening when doing tap on button
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
String currentDBPath = "/data/data/app.church.sophie/databases/churchdb.db";
String backupDBPath = "SAMPLE_DB_NAME";
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(ExportDataActivity.this, "DB Exported!", Toast.LENGTH_LONG).show();
} catch(IOException e) {
e.printStackTrace();
}
}
I am using the following code that was posted somewhere on Stack Overflow and modified for my purposes:
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//"+ "com.exercise.AndroidSQLite" +"//databases//"+"MY_DATABASE";
String backupDBPath = "/temp/MY_DATABASE";
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(getBaseContext(), backupDB.toString(), Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
}
}
So the error I get when I try to access it is:
java.io.FileNotFoundException: /data/data/com.exercise.AndroidSQLite/databases/MY_DATABASE: open failed: EACCES (Permission Denied)
I am trying to copy this file without rooting my tablet. The write external storage directory permission is set in the application; I just can't get around this error. Would appreciate help on resolving this issue, it's driving me mad
I am backing up my database in my android application and it works fine. You can only access the database file if you are the owner of it, meaning your application created it.
I think your path is wrong, I have this in my app:
private static final String DATABASE_NAME = "my.db.name";
public File getBackupDatabaseFile() {
File dir = new File(getStorageBaseDirectory().getAbsolutePath() + "/backup");
if (!dir.exists()) {
dir.mkdirs();
}
return new File(dir, DATABASE_NAME);
}
public final boolean backupDatabase() {
File from = mContext.getDatabasePath(DATABASE_NAME);
File to = this.getBackupDatabaseFile();
try {
FileUtils.copyFile(from, to);
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(TAG, "Error backuping up database: " + e.getMessage(), e);
}
return false;
}
And FileUtils.copyFIle is this:
public static void copyFile(File src, File dst) throws IOException {
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dst);
FileChannel fromChannel = null, toChannel = null;
try {
fromChannel = in.getChannel();
toChannel = out.getChannel();
fromChannel.transferTo(0, fromChannel.size(), toChannel);
} finally {
if (fromChannel != null)
fromChannel.close();
if (toChannel != null)
toChannel.close();
}
}
You are using "MY_DATABASE" literally when you probably actually want to use it as a variable...
Remove the quotes from around it and see if that doesn't solve your problem.