0

I have tried implementing the following code. I am using my emulator and it has sdcard storage space. I have inserted A.db using adb commands and I can see that the database is the specified path.

public void importDB() {

    String dir=Environment.getExternalStorageDirectory().getAbsolutePath();
    File sd = new File(dir);
    File data = Environment.getDataDirectory();
    FileChannel source = null;
    FileChannel destination = null;
    String backupDBPath = "/data/com.example.mine.move/databases/A.db";
    String currentDBPath = "A.db";
    File currentDB = new File(sd, currentDBPath);
    File backupDB = new File(data, backupDBPath);
    try {
        source = new FileInputStream(currentDB).getChannel();
        Toast.makeText(this,source.toString(),Toast.LENGTH_SHORT).show();
        destination = new FileOutputStream(backupDB).getChannel();
        destination.transferFrom(source, 0, source.size());
        source.close();
        destination.close();
        Toast.makeText(this, "Please wait", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
    }

But I keep getting the following error message:

java.io.FileNotFoundException: /storage/emulated/0/A.db: open failed: EACCES (Permission denied)

I have used the following permissions as well.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Prabha
  • 135
  • 12
  • If your `targetSdkVersion` is 23 or higher, make sure that you are handling runtime permissions. See https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it and https://developer.android.com/training/permissions/requesting.html – CommonsWare Jul 08 '16 at 15:57
  • Thank you @CommonsWare. I was able to import my database after downgrading my sdk. – Prabha Jul 11 '16 at 14:21

0 Answers0