public class ExportDatabaseTask extends AsyncTask<Void, Void, String> {
private Context mContext;
private final ProgressDialog dialog;
public ExportDatabaseTask(Context context){
this.mContext=context;
dialog = new ProgressDialog(mContext);
}
// can use UI thread here
@Override
protected void onPreExecute() {
this.dialog.setMessage("正在导出数据库文件至SD卡,请稍候...");
mContext.getResources().getString(R.string.app_name );
this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
@Override
protected String doInBackground(final Void... args) {
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
return "未找到SD卡";
}
//setting.db为apk里的一个数据库文件
File dbFile = new File(Environment.getExternalStorageDirectory().getPath()+"/contacts1.db");
//"goodboyenglish_setting.db"为要备份至SD卡上的数据库文件名称
File file =new File(Environment.getDataDirectory().getPath()+ "/data/ntc.xxxx/databases/student.db");
Log.e("1",dbFile.toString());
Log.e("1",file.toString());
try {
//创建一个文件,如果文件存在的话会自动把原来的覆盖掉
Log.e("1",file.toString());
dbFile.createNewFile();
dbFile.exists();
//拷贝文件
// copyFile(dbFile, file);
export(dbFile ,file);
Log.e("1",dbFile.toString());
return "成功导出数据库文件至SD卡!";
} catch (IOException e) {
e.printStackTrace();
return "导出失败!";
}
}
// can use UI thread here
@Override
protected void onPostExecute(final String msg) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
}
//拷贝文件的函数 src 为需要复制的文件,dst为目标文件(被src覆盖的文件)
//拷贝的过程其实就是把src文件里内容写入dst文件里的过程(从头写到尾)
/**sqlite 内部文件的复制
public static 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();
}
//调用方式:new ExportDatabaseTask().execute();
} */
//通过流操作将文件复制
private SQLiteDatabase database;
public SQLiteDatabase export(File src ,File dec) {
FileInputStream fis;
try {
fis = new FileInputStream(src);
FileOutputStream fos =new FileOutputStream(dec);
System.out.println("2.1");
byte[] buffer = new byte[8192];//这里为什么是8192我不清楚我对java一知半解,不懂,是不是文件的大小啊
System.out.println("3");
int count =0;
// 开始复制db文件
while ((count=fis.read(buffer))>0){
fos.write(buffer, 0, count);
System.out.println("4");
}
fos.close();
fis.close();
}catch(Exception e){
Toast.makeText(mContext, "fail", 10000);
}
}
}
注:清单权限问题
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
要是读取手机sqlite的内部db如data/data/com.android.proviers.contacts/databases/contacts2.db
需要权限<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
有时需要系统权限
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ntc.zms"
android:versionCode="1">
<!-- android:versionName="1.0" android:sharedUserId="system" -->//虚拟机以及手机非系统软件写了之后是无法运行的只能在系统文件中写
<uses-sdk android:minSdkVersion="10" />