我如何将我的sqllite数据库导出到SD卡

时间:2017-02-19 10:10:00

标签: java android sqlite android-sqlite database-backups

如何将sqlite数据库导出到外部存储器`我正在尝试此方法,但它显示异常

 public void exportdb() {


    Log.e("data ","export");
    File sd=Environment.getExternalStorageDirectory();
    File data = Environment.getDataDirectory();
    FileChannel source=null;
    FileChannel destination=null;
    String currentDBPath = "/data/"+ "com.example.kishababy.myapplication.DataBase" +"/databases/"+ "contactsManager";
    String backupDBPath = "contactsManager";
    File currentDB = new File(data, currentDBPath);
    File backupDB = new File(sd, backupDBPath);
    try {
        source = new FileInputStream(currentDB).getChannel();
        destination = new FileOutputStream(backupDB).getChannel();
        destination.transferFrom(source, 0, source.size());
        source.close();
        destination.close();
        Log.e("Toast","befor");

        Toast.makeText(getApplicationContext(), "DB Exported!", Toast.LENGTH_LONG).show();
    } catch(IOException e) {
        e.printStackTrace();
        Log.e("Exception",e.toString());
    }






}

品酒时的例外情况 enter image description here

3 个答案:

答案 0 :(得分:0)

我认为您需要为您的应用获取或授予lists:foldl权限(您的手机运行的是Android 7.0,这是我最好的猜测)

答案 1 :(得分:0)

可能你必须给出路径:

String currentDBPath = "//data//"+ "com.example.kishababy.myapplication.DataBase" +"//databases//"+ "contactsManager";

并确保您已添加

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></u‌​ses-permission>

首先检查你的SD卡状态:

String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
    Log.d("Test", "sdcard mounted and writable");
}
else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    Log.d("Test", "sdcard mounted readonly");
}
else {
    Log.d("Test", "sdcard state: " + state);
}

答案 2 :(得分:0)

使用

String currentDBPath = "/data/data/"+ "com.example.kishababy.myapplication.DataBase" +"/databases/"+ "contactsManager";

而不是

String currentDBPath = "/data/"+ "com.example.kishababy.myapplication.DataBase" +"/databases/"+ "contactsManager";

此外,您必须调用权限并确保在进行备份之前已创建数据库。

您可以使用以下完全集成:

  1. 将用户权限添加到清单文件中以进行存储

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
  2. 使用此代码段进行备份:

    public void exportDb() {
    try {
        String inFileName =  "/data/data/<com.abc.app>/databases/app.db";
    
        File dbFile = new File(inFileName);
        FileInputStream fis = new FileInputStream(dbFile);
    
        String outFileName = Environment.getExternalStorageDirectory() + "/app_copy.db";
    
        OutputStream output = new FileOutputStream(outFileName);
    
        byte[] buffer = new byte[1024];
        int length;
        while ((length = fis.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
    
        output.flush();
        output.close();
        fis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    }
    
  3. 检查存储权限并调用备份方法:

    private void backupData() {
    if ((ContextCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
        ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 11);
    } else {
        exportDb();
    }
    

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == 11) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            exportDb();
        }
    }
    }