Soldato
I'm having a problem with my backup and restore of a database in my Android App.
I have a few different problems. The first is it won't create the directory, even though I have write permissions -
The logcat is -
Then, with a backed up database that works fine on other phones, the import wont work either -
The logcat looks like this -
So no errors. It doesn't say it can't find or access the file, it just seems to ignore the contents of it?
Any ideas?
I have a few different problems. The first is it won't create the directory, even though I have write permissions -
Code:
public static void exportDB() {
Log.i("exportDB", "Starting");
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source = null;
FileChannel destination = null;
File dir = new File(Environment.getExternalStorageDirectory() + "/AutoBuddy/");
Log.i("dir is ", "" + dir);
try {
if (dir.mkdir()) {
System.out.println("Directory created");
} else {
System.out.println("Directory is not created");
}
} catch (Exception e) {
e.printStackTrace();
Log.i("Creating Dir Error", "" + e);
}
String currentDBPath = "/data/com.androidandyuk.autobuddy/databases/Vehicles";
String backupDBPath = "AutoBuddy/Vehicles.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
Context context = App.getContext();
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
Toast.makeText(context, "DB Exported!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.i("Export Failed", "Error");
e.printStackTrace();
Toast.makeText(context, "Export Failed!", Toast.LENGTH_LONG).show();
}
}
The logcat is -
Code:
2018-11-19 19:29:15.859 31659-31659/com.androidandyuk.autobuddy I/exportDB: Starting
2018-11-19 19:29:15.861 31659-31659/com.androidandyuk.autobuddy I/dir is: /storage/emulated/0/AutoBuddy
2018-11-19 19:29:15.861 31659-31659/com.androidandyuk.autobuddy I/System.out: Directory is not created
2018-11-19 19:29:15.861 31659-31659/com.androidandyuk.autobuddy I/Export Failed: Error
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: java.io.FileNotFoundException: /storage/emulated/0/AutoBuddy/Vehicles.db (Permission denied)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at java.io.FileOutputStream.open0(Native Method)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at java.io.FileOutputStream.open(FileOutputStream.java:308)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:238)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:180)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at com.androidandyuk.autobuddy.Settings.exportDB(Settings.java:311)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at com.androidandyuk.autobuddy.Garage.onNavigationItemSelected(Garage.java:1238)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:170)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at android.support.v7.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:90)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:352)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at android.view.View.performClick(View.java:6653)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at android.view.View.performClickInternal(View.java:6625)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at android.view.View.access$3100(View.java:786)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at android.view.View$PerformClick.run(View.java:26223)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at android.os.Handler.handleCallback(Handler.java:891)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at android.os.Looper.loop(Looper.java:207)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7470)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
2018-11-19 19:29:15.862 31659-31659/com.androidandyuk.autobuddy W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
Then, with a backed up database that works fine on other phones, the import wont work either -
Code:
public static void importDB() {
Log.i("ImportDB", "Started");
try {
String DB_PATH = "/data/data/com.androidandyuk.autobuddy/databases/Vehicles";
File sdcard = Environment.getExternalStorageDirectory();
String yourDbFileNamePresentInSDCard = sdcard.getAbsolutePath() + File.separator + "AutoBuddy/Vehicles.db";
Log.i("ImportDB", "SDCard File " + yourDbFileNamePresentInSDCard);
File file = new File(yourDbFileNamePresentInSDCard);
// Open your local db as the input stream
InputStream myInput = new FileInputStream(file);
// Path to created empty db
String outFileName = DB_PATH;
// Opened assets database structure
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
} catch (Exception e) {
Log.i("ImportDB", "Exception Caught" + e);
}
loadBikes();
Fuelling.loadFuels();
Maintenance.loadLogs();
ToDo.loadToDos();
Context context = App.getContext();
Toast.makeText(context, "Data Imported. Close app and reopen", Toast.LENGTH_LONG).show();
if (bikes.size() > 0) {
activeBike = 0;
}
}
public void importDB2() {
Uri selectedUri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/AutoBuddy/");
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setDataAndType(selectedUri, "*/*");
Intent i = Intent.createChooser(intent, "File");
startActivityForResult(i, CHOOSE_FILE_REQUESTCODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case CHOOSE_FILE_REQUESTCODE:
if(resultCode==-1){
Uri uri = data.getData();
String yourDbFileNamePresentInSDCard = uri.getPath();
//int index = yourDbFileNamePresentInSDCard.indexOf(":");
//if(index > 0) {
// yourDbFileNamePresentInSDCard = yourDbFileNamePresentInSDCard.substring(index+1);
//}
Log.i("ImportDB", "Started");
try {
String DB_PATH = "/data/data/com.androidandyuk.autobuddy/databases/sessions";
// File sdcard = Environment.getExternalStorageDirectory();
// yourDbFileNamePresentInSDCard = sdcard.getAbsolutePath() + File.separator + "LapTimerBuddy/LapTimer.db";
Log.i("ImportDB", "SDCard File " + yourDbFileNamePresentInSDCard);
File file = new File(yourDbFileNamePresentInSDCard);
// Open your local db as the input stream
InputStream myInput = new FileInputStream(file);
// Path to created empty db
String outFileName = DB_PATH;
// Opened assets database structure
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
} catch (Exception e) {
Log.i("ImportDB", "Exception Caught" + e);
}
loadBikes();
Fuelling.loadFuels();
Maintenance.loadLogs();
ToDo.loadToDos();
Context context = App.getContext();
Toast.makeText(context, "Data Imported. Close app and reopen", Toast.LENGTH_LONG).show();
if (bikes.size() > 0) {
activeBike = 0;
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
The logcat looks like this -
Code:
2018-11-19 19:39:04.036 3348-3348/com.androidandyuk.autobuddy I/ImportDB: Started
2018-11-19 19:39:04.037 3348-3348/com.androidandyuk.autobuddy I/ImportDB: SDCard File /storage/emulated/0/AutoBuddy/Vehicles.db
2018-11-19 19:39:04.039 3348-3348/com.androidandyuk.autobuddy I/Main Activity: New Bikes Loading
2018-11-19 19:39:04.039 3348-3348/com.androidandyuk.autobuddy I/Bikes Size: 0
2018-11-19 19:39:04.044 3348-3348/com.androidandyuk.autobuddy I/Bikes Restored: Count :0
2018-11-19 19:39:04.044 3348-3348/com.androidandyuk.autobuddy I/Retrieved info: Log count :0
2018-11-19 19:39:04.124 3348-3348/com.androidandyuk.autobuddy D/HwAppInnerBoostImpl: asyncReportData com.androidandyuk.autobuddy,2,2,1,4 interval=206
2018-11-19 19:39:04.124 3348-3348/com.androidandyuk.autobuddy I/ViewRootImpl: jank_removeInvalidNode all the node in jank list is out of time
So no errors. It doesn't say it can't find or access the file, it just seems to ignore the contents of it?
Any ideas?
Last edited: