将数据库从SD卡导入到Android应用程序

时间:2016-07-08 15:53:24

标签: android sqlite android-studio android-sqlite android-permissions

我尝试过实现以下代码。我正在使用我的模拟器,它有SD卡存储空间。我使用adb命令插入了A.db,我可以看到数据库是指定的路径。

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();
    }

但我不断收到以下错误消息:

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

我也使用了以下权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

0 个答案:

没有答案