使数据库备份到SD卡上的Android [英] Making a database backup to SDCard on Android
本文介绍了使数据库备份到SD卡上的Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是下面的code写一个备份到SD卡,我得到
java.io.IOException异常:文件的上级目录不可写:/sdcard/mydbfile.db
私有类ExportDatabaseFileTask扩展的AsyncTask<字符串,太虚,布尔> {
私人最终ProgressDialog对话框=新ProgressDialog(CTX);
//这里可以使用UI线程
在preExecute保护无效(){
this.dialog.setMessage(导出数据库......);
this.dialog.show();
}
//自动辅助线程上完成(独立于UI线程)
保护布尔doInBackground(最后的字符串参数... args){
文件DBFILE =
新的文件(Environment.getDataDirectory()+/data/com.mypkg/databases/mydbfile.db);
文件exportDir =新的文件(Environment.getExternalStorageDirectory(),);
如果(!exportDir.exists()){
exportDir.mkdirs();
}
档案文件=新的文件(exportDir,dbFile.getName());
尝试 {
file.createNewFile();
this.copyFile(DBFILE,文件);
返回true;
}赶上(IOException异常E){
Log.e(mypck,e.getMessage(),E);
返回false;
}
}
//这里可以使用UI线程
保护无效onPostExecute(最终布尔成功){
如果(this.dialog.isShowing()){
this.dialog.dismiss();
}
如果(成功){
Toast.makeText(CTX,导出成功了!,Toast.LENGTH_SHORT).show();
} 其他 {
Toast.makeText(CTX,导出失败,Toast.LENGTH_SHORT).show();
}
}
无效的CopyFile(SRC文件,文件DST)抛出IOException异常{
FileChannel随路=新的FileInputStream(SRC).getChannel();
FileChannel outChannel =新的FileOutputStream(DST).getChannel();
尝试 {
inChannel.transferTo(0,inChannel.size(),outChannel);
} 最后 {
如果(随路!= NULL)
inChannel.close();
如果(outChannel!= NULL)
outChannel.close();
}
}
}
解决方案
你有没有在清单中定义的权限?
<使用,许可
机器人:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
I am using the below code to write a backup copy to SDCard and I get
java.io.IOException: Parent directory of file is not writable: /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();
}
}
}
解决方案
Do you have permissions defined in manifest ?
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
这篇关于使数据库备份到SD卡上的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文