我一直在尝试创建应用程序数据库的副本并将其保存到手机的根目录,但没有运气。我的日志去:
第一步
绝地求生
显然if (sd.canWrite())
是第一个错误?有人能帮忙吗?我的想法不多了。谢谢
public void save() {
File data = Environment.getDataDirectory();
File sd = new File(Environment.getExternalStorageDirectory().toString());
sd.mkdirs();
Log.d("XXXXXXXXXXXXXXXXXXX", "STEP ONE");
if (sd.canWrite()) {
Log.d("XXXXXXXXXXXXXXXXXXX", "STEP TWO");
String currentDBPath = "/data/com.test.example/databases/example_db";
String backupDBPath = "/mytest" + "version_two" + ".db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
Log.d("XXXXXXXXXXXXXXXXXXX", "STEP THREE");
if (currentDB.exists()) {
Log.d("XXXXXXXXXXXXXXXXXXX", "STEP FOUR");
FileChannel src;
try {
Log.d("XXXXXXXXXXXXXXXXXXX", "STEP FIVE");
src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB)
.getChannel();
try {
Log.d("XXXXXXXXXXXXXXXXXXX", "STEP SIX");
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
} catch (IOException e) {
Log.d("XXXXXXXXXXXXXXXXXXX", "STEP ONE DEAD");
e.printStackTrace();
}
} catch (FileNotFoundException e) {
Log.d("XXXXXXXXXXXXXXXXXXX", "STEP TWO DEAD");
e.printStackTrace();
}
}
}else{
Log.d("XXXXXXXXXXXXXXXXXXX", "STEP DEAD");
}
}
我的清单剪辑 - 我不知道怎么android:allowBackup="true"
到那里
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
在模拟器输出上:
01-13 14:16:13.082: D/XXXXXXXXXXXXXXXXXXX(30105): STEP ONE
01-13 14:16:13.082: D/XXXXXXXXXXXXXXXXXXX(30105): STEP DEAD
在设备输出上:
01-13 14:16:13.082: D/XXXXXXXXXXXXXXXXXXX(30105): STEP ONE
01-13 14:16:13.082: D/XXXXXXXXXXXXXXXXXXX(30105): STEP TWO
01-13 14:16:13.082: D/XXXXXXXXXXXXXXXXXXX(30105): STEP THREE
01-13 14:16:13.082: D/XXXXXXXXXXXXXXXXXXX(30105): STEP FOUR
01-13 14:16:13.082: D/XXXXXXXXXXXXXXXXXXX(30105): STEP FIVE
01-13 14:16:13.090: D/XXXXXXXXXXXXXXXXXXX(30105): STEP SIX