Skip to content

Commit 2f4c067

Browse files
committed
[NewFeature] Add database export util.
1 parent 10ffe18 commit 2f4c067

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Copyright 2014 Zhenguo Jin (jingle1267@163.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.worthed.utils;
17+
18+
import android.content.Context;
19+
import android.os.Environment;
20+
import android.text.TextUtils;
21+
22+
/**
23+
* 应用数据库导出工具类
24+
*
25+
* @author jingle1267@163.com
26+
*
27+
*/
28+
public class DatabaseExportUtils {
29+
30+
private static final boolean DEBUG = true;
31+
private static final String TAG = "DatabaseExportUtils";
32+
33+
/**
34+
* 开始导出数据 此操作比较耗时,建议在线程中进行
35+
*
36+
* @param context
37+
* @param targetFile
38+
* 目标文件
39+
* @param databaseName
40+
* 要拷贝的数据库文件名
41+
* @return
42+
*/
43+
public boolean startExportDatabase(Context context, String targetFile,
44+
String databaseName) {
45+
if (DEBUG) {
46+
LogUtils.d(TAG, "start export ...");
47+
}
48+
if (!Environment.MEDIA_MOUNTED.equals(Environment
49+
.getExternalStorageState())) {
50+
if (DEBUG) {
51+
LogUtils.w(TAG, "cannot find SDCard");
52+
}
53+
return false;
54+
}
55+
String sourceFilePath = Environment.getDataDirectory() + "/data/"
56+
+ context.getPackageName() + "/databases/" + databaseName;
57+
String destFilePath = Environment.getExternalStorageDirectory()
58+
+ (TextUtils.isEmpty(targetFile) ? (context.getPackageName() + ".db")
59+
: targetFile);
60+
boolean isCopySuccess = FileUtils
61+
.copyFile(sourceFilePath, destFilePath);
62+
if (DEBUG) {
63+
if (isCopySuccess) {
64+
LogUtils.d(TAG, "copy database file success. target file : "
65+
+ destFilePath);
66+
} else {
67+
LogUtils.w(TAG, "copy database file failure");
68+
}
69+
}
70+
return isCopySuccess;
71+
}
72+
}

0 commit comments

Comments
 (0)