Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android上对SD卡进行数据库备份_Android - Fatal编程技术网

在Android上对SD卡进行数据库备份

在Android上对SD卡进行数据库备份,android,Android,我正在使用下面的代码将备份副本写入SD卡,我得到 java.io.IOException:文件的父目录不可写:/sdcard/mydbfile.db private class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> { private final ProgressDialog dialog = new ProgressDialog(ctx); // can us

我正在使用下面的代码将备份副本写入SD卡,我得到

java.io.IOException:文件的父目录不可写:/sdcard/mydbfile.db

private class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {
        private final ProgressDialog dialog = new ProgressDialog(ctx);

        // can use UI thread here
        protected void onPreExecute() {
           this.dialog.setMessage("Exporting database...");
           this.dialog.show();
        }

        // automatically done on worker thread (separate from UI thread)
        protected Boolean doInBackground(final String... args) {

           File dbFile =
                    new File(Environment.getDataDirectory() + "/data/com.mypkg/databases/mydbfile.db");

           File exportDir = new File(Environment.getExternalStorageDirectory(), "");
           if (!exportDir.exists()) {
              exportDir.mkdirs();
           }
           File file = new File(exportDir, dbFile.getName());

           try {
              file.createNewFile();
              this.copyFile(dbFile, file);
              return true;
           } catch (IOException e) {
              Log.e("mypck", e.getMessage(), e);
              return false;
           }
        }

        // can use UI thread here
        protected void onPostExecute(final Boolean success) {
           if (this.dialog.isShowing()) {
              this.dialog.dismiss();
           }
           if (success) {
              Toast.makeText(ctx, "Export successful!", Toast.LENGTH_SHORT).show();
           } else {
              Toast.makeText(ctx, "Export failed", Toast.LENGTH_SHORT).show();
           }
        }

        void copyFile(File src, File dst) throws IOException {
           FileChannel inChannel = new FileInputStream(src).getChannel();
           FileChannel outChannel = new FileOutputStream(dst).getChannel();
           try {
              inChannel.transferTo(0, inChannel.size(), outChannel);
           } finally {
              if (inChannel != null)
                 inChannel.close();
              if (outChannel != null)
                 outChannel.close();
           }
        }

     }
私有类ExportDatabaseFileTask扩展了AsyncTask{
私有最终ProgressDialog=新建ProgressDialog(ctx);
//可以在这里使用UI线程
受保护的void onPreExecute(){
this.dialog.setMessage(“导出数据库…”);
this.dialog.show();
}
//在工作线程上自动完成(与UI线程分开)
受保护的布尔doInBackground(最终字符串…args){
文件dbFile=
新文件(Environment.getDataDirectory()+“/data/com.mypkg/databases/mydbfile.db”);
File exportDir=新文件(Environment.getExternalStorageDirectory(),“”);
如果(!exportDir.exists()){
exportDir.mkdirs();
}
File File=新文件(exportDir,dbFile.getName());
试一试{
createNewFile();
这个.copyFile(dbFile,file);
返回true;
}捕获(IOE异常){
Log.e(“mypck”,e.getMessage(),e);
返回false;
}
}
//可以在这里使用UI线程
受保护的void onPostExecute(最终布尔值成功){
if(this.dialog.isShowing()){
this.dialog.disclose();
}
如果(成功){
Toast.makeText(ctx,“导出成功!”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(ctx,“导出失败”,Toast.LENGTH_SHORT).show();
}
}
void copyFile(文件src、文件dst)引发IOException{
FileChannel inChannel=newfileinputstream(src).getChannel();
FileChannel outChannel=新的FileOutputStream(dst).getChannel();
试一试{
inChannel.transferTo(0,inChannel.size(),outChannel);
}最后{
如果(inChannel!=null)
inChannel.close();
如果(输出通道!=null)
outChannel.close();
}
}
}

您是否在清单中定义了权限

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


非常好的帖子,谢谢。我验证了该代码是否可以将数据库复制到sd卡上。您的问题对我来说是一个解决方案。Thanx!;)这是个好把戏。但我想知道如何恢复备份…很好+给你100美元:)