-1

I am trying to import an sqlite database so that is overrites the one that is currently there. I have managed to export the database to a file in a folder on the phone, but when i try to import it i get the Exception that says " no such file or directory". How do i fix it?

This is my code to export the database to a file

private void exportDB() {
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String  currentDBPath= "//data//" + "com.varcon.campus"
                    + "//databases//" + "CampusTime.db";
            String backupDBPath  = "/Download/CampusTime";
            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();

    }
}

The file is successfully exported to a folder with its name This is my code to import the database

int PICKFILE_RESULT_CODE = 1;
            Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
            chooseFile.setType("*/*");
            chooseFile = Intent.createChooser(chooseFile, "Choose a file");
            startActivityForResult(chooseFile, PICKFILE_RESULT_CODE);


@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent datas) {
    Uri uri = datas.getData();
    String filePath = uri.getPath();
    try {
        //Toast.makeText(this, ""+src, Toast.LENGTH_LONG).show();

        try {
            File sd = Environment.getExternalStorageDirectory();
            File data  = Environment.getDataDirectory();

            if (sd.canWrite()) {
                String  currentDBPath= "//data//" + "com.varcon.campus"
                        + "//databases//" + "CampusTime.db";
                String backupDBPath  = filePath;
                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(getBaseContext(), backupDB.toString(),
                        Toast.LENGTH_LONG).show();

            }
        } catch (Exception e) {

            Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
                    .show();

        }

        //new CustomMessage(getActivity(), "Database replaced sucessfully");
    } catch (Exception e) {
        e.printStackTrace();
        //CustomLog.showLogD("WORKING_STOP",e.getMessage());
    }
    super.onActivityResult(requestCode, resultCode, datas);
}

And i have permissions in my manifest available

Stacktrace :

2020-03-10 23:14:43.704 31088-31088/com.varcon.campus D/error: java.io.FileNotFoundException: /storage/emulated/0/Download/CampusTime (No such file or directory)
Dwayne T
  • 95
  • 2
  • 8
  • It's very difficult to debug a crash without a stack trace. Please see [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) for Android-specific advice, and [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) for advice on what to do once you have the stack trace. If you still need help, please edit your question to include the stack trace. – Ryan M Mar 10 '20 at 21:09
  • stack trace added @RyanMentley – Dwayne T Mar 10 '20 at 21:18
  • that's not a stack trace – Ryan M Mar 10 '20 at 21:51
  • `String filePath = uri.getPath();`. That is not a file path. – blackapps Mar 10 '20 at 22:18

1 Answers1

0

The only thing you have to do is to open an InputStream for the picked uri instead of using a FileInputStream for a file.

InputStream is = getContentResolver().openInputStream(data.getData());

Then read from the stream as usual.

blackapps
  • 8,011
  • 2
  • 11
  • 25