Android导出APK里的数据库.db文件至SD卡
2013-01-07 17:11
525 查看
// 参考: http://code.google.com/p/and-examples/source/browse/#svn/trunk/database/src/com/totsp/database
private class ExportDatabaseTask extends AsyncTask<Void, Void, String> {
private final ProgressDialog dialog = new ProgressDialog(mContext);
// can use UI thread here
@Override
protected void onPreExecute() {
this.dialog.setMessage("正在导出数据库文件至SD卡,请稍候...");
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.getDataDirectory() + "/data/com.goodboyenglish.leo/databases/setting.db");
//"goodboyenglish_setting.db"为要备份至SD卡上的数据库文件名称
File file = new File(Environment.getExternalStorageDirectory(), "goodboyenglish_setting.db");
try {
//创建一个文件,如果文件存在的话会自动把原来的覆盖掉
file.createNewFile();
//拷贝文件
copyFile(dbFile, file);
return "成功导出数据库文件至SD卡!";
} catch (IOException e) {
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文件里的过程(从头写到尾)
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 class ExportDatabaseTask extends AsyncTask<Void, Void, String> {
private final ProgressDialog dialog = new ProgressDialog(mContext);
// can use UI thread here
@Override
protected void onPreExecute() {
this.dialog.setMessage("正在导出数据库文件至SD卡,请稍候...");
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.getDataDirectory() + "/data/com.goodboyenglish.leo/databases/setting.db");
//"goodboyenglish_setting.db"为要备份至SD卡上的数据库文件名称
File file = new File(Environment.getExternalStorageDirectory(), "goodboyenglish_setting.db");
try {
//创建一个文件,如果文件存在的话会自动把原来的覆盖掉
file.createNewFile();
//拷贝文件
copyFile(dbFile, file);
return "成功导出数据库文件至SD卡!";
} catch (IOException e) {
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文件里的过程(从头写到尾)
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();
相关文章推荐
- Android导出APK里的数据库.db文件至SD卡
- 解决:Android使用自带sqlite开发时,apk中创建的数据库外部的进程是没有权限去读/写的,而且无法如何读取指定目录下的db文件
- android 代码将数据库文件导出到sd卡
- 把数据库db文件放在了sd卡中,和apk在不同的文件夹里
- Android开发 SQLite 通过.db文件导入已有数据库
- Unity3d导出Android的apk文件时相关问题的解决办法
- android 导出签名APK--混淆文件proguard.cfg详解
- dos 安装android apk 以及导出系统文件(手机需root)到本地磁盘
- Android数据库SQLite文件打包,随APK发布
- 将android程序中的数据库导出到SD卡
- Android如何导入已有的外部数据库(在raw下自己导入db文件)
- Android数据库(SqlLite)操作和db文件查看
- 如何从Eclipse中导出数据库文件.db
- android导出apk文件时遇到的问题
- Android 短信数据库与联系人数据下的.db-wal文件
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- Android游戏开发13:SQLite存储数据及保存SQLite数据库文件到SD卡
- 导入导出手机的Android的项目的数据库文件的bat文件编写
- 在Eclipse中导出带有自己签名的Android apk文件
- Android 将.sql格式文件转换为.db格式数据库文件