1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Android Database Restore

Discussion in 'HTML, Graphics & Programming' started by AndyCr15, Nov 19, 2018.

  1. AndyCr15

    Sgarrista

    Joined: Apr 28, 2011

    Posts: 7,979

    Location: London, UK

    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 -

    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: Nov 19, 2018
  2. jsmoke

    Soldato

    Joined: Jun 17, 2012

    Posts: 6,080

    What dev tools are you using can you not debug it?

    First guess is a path error, try a forward slash before AutoBuddy/Vehicles.db, that's just from a very quick glance.
     
  3. AndyCr15

    Sgarrista

    Joined: Apr 28, 2011

    Posts: 7,979

    Location: London, UK

    I use Android Studio. The logcat is what I would usually use to see where it's failing, but I don't see why, hence I post them here.

    The + File.separator + puts the correct file separator. I should probably change out other / for it too actually.
     
  4. jsmoke

    Soldato

    Joined: Jun 17, 2012

    Posts: 6,080

  5. AndyCr15

    Sgarrista

    Joined: Apr 28, 2011

    Posts: 7,979

    Location: London, UK

    Fantastic, thanks. That did it. It doesn't actually ask for permissions again, but I guess it's the fact it checks it still has them at run time.

    I can export and import on the Mi A2 Lite now (running stock) but the Huawei still wont import the perfectly fine database. The logcat is -

    Code:
    2018-11-22 09:43:07.358 13024-13024/com.androidandyuk.autobuddy I/ViewRootImpl: jank_removeInvalidNode all the node in jank list is out of time
    2018-11-22 09:43:07.362 13024-13024/com.androidandyuk.autobuddy V/AudioManager: playSoundEffect   effectType: 0
    2018-11-22 09:43:07.362 13024-13024/com.androidandyuk.autobuddy V/AudioManager: querySoundEffectsEnabled...
    2018-11-22 09:43:07.363 13024-13024/com.androidandyuk.autobuddy I/ImportDB: Started
    2018-11-22 09:43:07.364 13024-13024/com.androidandyuk.autobuddy I/ImportDB: SDCard File /storage/emulated/0/AutoBuddy/Vehicles.db
    2018-11-22 09:43:07.366 13024-13024/com.androidandyuk.autobuddy I/Main Activity: New Bikes Loading
    2018-11-22 09:43:07.366 13024-13024/com.androidandyuk.autobuddy I/Bikes Size: 0
    2018-11-22 09:43:07.378 13024-13024/com.androidandyuk.autobuddy I/Bikes Restored: Count :0
    2018-11-22 09:43:07.378 13024-13024/com.androidandyuk.autobuddy I/Retrieved info: Log count :0
    2018-11-22 09:43:07.397 13024-13024/com.androidandyuk.autobuddy D/HwAppInnerBoostImpl: asyncReportData com.androidandyuk.autobuddy,2,1,1,0 interval=159
    2018-11-22 09:43:07.469 13024-13024/com.androidandyuk.autobuddy D/HwAppInnerBoostImpl: asyncReportData com.androidandyuk.autobuddy,2,2,1,6 interval=231
    2018-11-22 09:43:07.472 13024-13089/com.androidandyuk.autobuddy D/OpenGLRenderer:   HWUI Binary is  enabled
       
        --------- beginning of system
    2018-11-22 09:43:07.485 13024-13089/com.androidandyuk.autobuddy D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
    2018-11-22 09:43:07.485 13024-13089/com.androidandyuk.autobuddy D/OpenGLRenderer:   HWUI Binary is  enabled
    2018-11-22 09:43:07.654 13024-13024/com.androidandyuk.autobuddy D/HwAppInnerBoostImpl: asyncReportData com.androidandyuk.autobuddy,2,1,2,0 interval=416
    2018-11-22 09:43:10.914 13024-13089/com.androidandyuk.autobuddy W/libEGL: EGLNativeWindowType 0x76f41cf010 disconnect failed
    2018-11-22 09:43:10.928 1235-2380/? W/NotificationService: Toast already killed. pkg=com.androidandyuk.autobuddy callback=android.app.ITransientNotification$Stub$Proxy@2031e32
    2018-11-22 09:43:15.404 1235-1659/? D/hw_netstat: total/5999/1982,com.google.android.keep/1891/1686,com.google.uid.shared:10015/2250/52,unknown:0/1570/0,com.android.vending/208/192,com.androidandyuk.autobuddy/80/52
    2018-11-22 09:43:30.423 1235-1659/? D/hw_netstat: total/3866/1426,unknown:0/2252/80,com.google.uid.shared:10015/999/413,com.whatsapp/40/457,unknown:1051/341/67,com.huawei.appmarket/182/222,com.teslacoilsw.launcher/0/135,com.androidandyuk.autobuddy/52/52
    Anyone with any ideas?
     
    Last edited: Nov 22, 2018
  6. jsmoke

    Soldato

    Joined: Jun 17, 2012

    Posts: 6,080

    Perfectly find?

    So it does load the dB but finds nothing in it.

    Some setting or corrupt dB file maybe.
     
  7. AndyCr15

    Sgarrista

    Joined: Apr 28, 2011

    Posts: 7,979

    Location: London, UK

    Sorry, perfectly fine. The same db will work on other phones, just not the Huawei. I have deleted and downloaded a few times, in case of corrupt file, but seems to be fine.
     
  8. jsmoke

    Soldato

    Joined: Jun 17, 2012

    Posts: 6,080

  9. AndyCr15

    Sgarrista

    Joined: Apr 28, 2011

    Posts: 7,979

    Location: London, UK

    That's actually really interesting thanks, but no, I don't think it applies. That is about the really aggressive RAM management and how it shuts apps down dead when they are no longer in focus.
     
  10. jsmoke

    Soldato

    Joined: Jun 17, 2012

    Posts: 6,080

    There are not really any errors in your log. Must be some setting. How are you running the APK, just copying it onto the phone?

    DoDyo have install from unknown sources enabled but that would open a dialog warning you if not.
     
  11. AndyCr15

    Sgarrista

    Joined: Apr 28, 2011

    Posts: 7,979

    Location: London, UK

    That's like permissions in the app, it asks at the time. It's currently installed from Android Studio. I've just exported a signed apk but that's doing the same thing.

    I'm risking abuse and down voting, but I've put it on StackOverflow.
     
  12. jsmoke

    Soldato

    Joined: Jun 17, 2012

    Posts: 6,080

  13. AndyCr15

    Sgarrista

    Joined: Apr 28, 2011

    Posts: 7,979

    Location: London, UK

    Hmm, I don't think that's it... I really appreciate your efforts to find the solution though!

    For one, I don't have an option to change my default location like they show in the thread.

    Also, it seems to find the file no problem. I can backup and it creates a file in the correct folder. I guess the only issue could be if it's trying to store the database in a different internal folder, but I'm not sure how I check that when I'm not rooted? In fact, backing up the Huawei database and trying to restore that on the Android One device doesn't seem to work, so I guess the issue is it having problems storing the database internally, not the reading and writing of it during backup and restore.
     
  14. AndyCr15

    Sgarrista

    Joined: Apr 28, 2011

    Posts: 7,979

    Location: London, UK

    Everything looks to be okay when reading and writing the database internally. How can I check this line is correct -

    Code:
    String DB_PATH = "/data/data/com.androidandyuk.autobuddy/databases/Vehicles";
    If I don't have root? Can I use ADB to snoop round and check that is where the Huawei stores the app data?
     
  15. jsmoke

    Soldato

    Joined: Jun 17, 2012

    Posts: 6,080

    You should be able to see the file structure without root using something like ES explorer just won't be able to edit/ delete any files. Been a while.
     
  16. AndyCr15

    Sgarrista

    Joined: Apr 28, 2011

    Posts: 7,979

    Location: London, UK

    i found it using adb... all seems to be okay in there, then file is where it should be... so I'm rather stumped :(