一定要加上权限,否则不能在sdcard中创建文件
<!--在sdcard中创建/删除文件的权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!--往sdcard中写入数据的权限 -->
直接调用 restoreDB 进行数据库恢复,调用backupDB进行数据库备份。
恢复的时候会显示出可以恢复的数据库文件列表
<!--在sdcard中创建/删除文件的权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!--往sdcard中写入数据的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
直接调用 restoreDB 进行数据库恢复,调用backupDB进行数据库备份。
恢复的时候会显示出可以恢复的数据库文件列表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.nio.channels.FileChannel;
import
java.text.SimpleDateFormat;
import
java.util.Date;
import
android.app.AlertDialog;
import
android.content.Context;
import
android.content.DialogInterface;
import
android.content.DialogInterface.OnClickListener;
import
android.os.AsyncTask;
import
android.os.Environment;
import
android.view.ViewGroup.LayoutParams;
import
android.view.Window;
import
android.view.WindowManager;
import
android.widget.ProgressBar;
import
android.widget.Toast;
public
class
BackupAndRestore {
private
Context mContext =
null
;
private
String[] fileList =
null
;
// 数据库文件列表
private
int
choicePostion = -
3
;
// 选择数据库列表中的位置
private
AlertDialog dialog =
null
;
private
String BACK_FOLDER =
"backup"
;
private
String appName =
"myApp"
;
public
BackupAndRestore(Context context) {
mContext = context;
}
/**
* 恢复数据的Dialog
*/
public
void
restoreDB() {
fileList = getFileList();
AlertDialog.Builder builder =
new
AlertDialog.Builder(mContext);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle(
"恢复"
);
builder.setSingleChoiceItems(getFileList(), -
1
,
new
DialogClick());
builder.setPositiveButton(
"确定"
,
new
DialogClick());
builder.setNegativeButton(
"取消"
,
new
DialogClick());
builder.show();
}
/**
* 备份数据库
*/
public
void
backupDB() {
showDialog(
"是否备份数据库"
,
'B'
);
}
/**
* 显示一个Dialog
*
* @param title
* 标题 ,必须引用资源ID resource ID
* @param sign
* 根据标示调用方法 I - 恢复默认设置 D - 恢复默认设置 H -选择主机
*/
private
void
showDialog(String title,
char
sign) {
final
char
s = sign;
new
AlertDialog.Builder(mContext).setTitle(title)
.setPositiveButton(
"确定"
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialogI,
int
which) {
switch
(s) {
case
'B'
:
// 备份数据库
if
(dialog ==
null
) {
dialog = awaitDialog(mContext);
}
else
{
dialog.show();
}
new
ExecuteTask().execute(
'B'
);
break
;
default
:
break
;
}
}
}).setNegativeButton(
"取消"
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
}
}).show();
}
/**
* 备份操作
*
* @return
*/
private
boolean
backUp() {
boolean
isOk =
false
;
String sp = File.separator;
File sdFile = sdCardOk();
if
(sdFile !=
null
) {
try
{
String[] dbNames = {
"数据库名称"
};
// 创建日期文件夹
String folder_date = datePrefix();
File f =
new
File(sdFile.getAbsolutePath() + sp + folder_date);
if
(!f.exists()) {
f.mkdirs();
}
for
(
int
i =
0
; i < dbNames.length; i++) {
String dbName = dbNames[i];
File dbFile = dbOk(dbName);
if
(dbFile !=
null
) {
File backFile =
new
File(f.getAbsolutePath() + sp
+ dbFile.getName());
backFile.createNewFile();
isOk = fileCopy(backFile, dbFile.getAbsoluteFile());
if
(!isOk) {
break
;
}
}
}
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return
isOk;
}
/**
* 时间前缀
*
* @return
*/
private
String datePrefix() {
SimpleDateFormat format =
new
SimpleDateFormat(
"yyyy-MM-dd-HH-mm-ss"
);
Date date =
new
Date(System.currentTimeMillis());
String str = format.format(date);
return
str;
}
/**
* 文件夹列表
*
* @return
*/
private
String[] getFileList() {
String[] fileList =
null
;
File file = sdCardOk();
if
(file !=
null
) {
File[] list = file.listFiles();
if
(list !=
null
&& list.length >
0
) {
fileList =
new
String[list.length];
for
(
int
i =
0
; i < list.length; i++) {
fileList[i] = list[i].getName();
}
}
}
return
fileList;
}
/**
* sdCard是否存在 备份的文件夹是否存在
*
* @return null不能使用
*/
private
File sdCardOk() {
File bkFile =
null
;
String state = Environment.getExternalStorageState();
if
(Environment.MEDIA_MOUNTED.equals(state)) {
String sp = File.separator;
String backUpPath = Environment.getExternalStorageDirectory() + sp
+ appName + sp + BACK_FOLDER;
bkFile =
new
File(backUpPath);
if
(!bkFile.exists()) {
bkFile.mkdirs();
}
else
return
bkFile;
}
else
Toast.makeText(mContext,
"Sdcard 不存在"
, Toast.LENGTH_SHORT).show();
return
bkFile;
}
/**
* 恢复数据库
*
* @param name
* 选择的文件名称 选中的数据库名称
* @param resoreDbName
* 需要恢复的数据库名称
* @return
*/
public
boolean
restore(String name, File f) {
boolean
isOk =
false
;
if
(f !=
null
) {
File dbFile = dbOk(name);
try
{
// System.out.println("覆盖的名称"+dbName);
if
(dbFile !=
null
) {
isOk = fileCopy(dbFile, f.getAbsoluteFile());
}
else
isOk =
false
;
}
catch
(IOException e) {
e.printStackTrace();
}
}
return
isOk;
}
/**
* 数据库文件是否存在,并可以使用
*
* @return
*/
private
File dbOk(String dbName) {
String sp = File.separator;
String absPath = Environment.getDataDirectory().getAbsolutePath();
String pakName = mContext.getPackageName();
String dbPath = absPath + sp +
"data"
+ sp + pakName + sp +
"databases"
+ sp + dbName;
File file =
new
File(dbPath);
if
(file.exists()) {
return
file;
}
else
{
return
null
;
}
}
/**
* 等候动画
*/
public
AlertDialog awaitDialog(Context context) {
ProgressBar bar =
new
ProgressBar(context);
bar.setLayoutParams(
new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
AlertDialog dialog =
new
AlertDialog.Builder(context).create();
dialog.setCancelable(
false
);
dialog.show();
Window window = dialog.getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.width =
50
;
params.height =
50
;
window.setAttributes(params);
window.setContentView(bar);
return
dialog;
}
/**
*
* @param outFile
* 写入
* @param inFile
* 读取
* @throws FileNotFoundException
*/
private
boolean
fileCopy(File outFile, File inFile)
throws
IOException {
if
(outFile ==
null
|| inFile ==
null
) {
return
false
;
}
boolean
isOk =
true
;
FileChannel inChannel =
new
FileInputStream(inFile).getChannel();
// 只读
FileChannel outChannel =
new
FileOutputStream(outFile).getChannel();
// 只写
try
{
long
size = inChannel.transferTo(
0
, inChannel.size(), outChannel);
if
(size <=
0
) {
isOk =
false
;
}
}
catch
(IOException e) {
isOk =
false
;
e.printStackTrace();
}
finally
{
if
(inChannel !=
null
) {
inChannel.close();
}
if
(outChannel !=
null
) {
outChannel.close();
}
}
return
isOk;
}
private
class
DialogClick
implements
DialogInterface.OnClickListener {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
if
(which == -
1
) {
// 确定
if
(choicePostion <
0
) {
Toast.makeText(mContext,
"选择数据库"
, Toast.LENGTH_SHORT)
.show();
return
;
}
String sp = File.separator;
String folderName = fileList[choicePostion];
String backUpPath = Environment.getExternalStorageDirectory()
+ sp + appName + sp + BACK_FOLDER + sp + folderName;
File file =
new
File(backUpPath);
if
(file.isDirectory()) {
File[] files = file.listFiles();
boolean
isOk =
false
;
for
(
int
i =
0
; i < files.length; i++) {
File f = files[i];
isOk = restore(f.getName(), f);
if
(!isOk) {
String fail_msg =
"恢复失败"
+
":"
+ f.getName();
Toast.makeText(mContext, fail_msg,
Toast.LENGTH_SHORT).show();
return
;
}
}
if
(isOk) {
// 如果有数据体现则需要刷新出新的数据
}
}
}
else
if
(which == -
2
) {
// 取消
}
else
if
(which >=
0
) {
choicePostion = which;
}
}
}
/**
* 执行任务
*
* @author Administrator
*
*/
private
class
ExecuteTask
extends
AsyncTask<Character, Void, Boolean> {
@Override
protected
Boolean doInBackground(Character... params) {
char
c = params[
0
];
if
(c ==
'B'
) {
backUp();
}
return
null
;
}
@Override
protected
void
onPostExecute(Boolean result) {
super
.onPostExecute(result);
if
(dialog !=
null
) {
dialog.dismiss();
}
}
}
}
|