dhaag23..
10
除非您的手机已植根,否则无法访问内部存储空间.一种简单的方法是使用模拟器,然后您可以获取文件.为了从设备上获取数据库,我编写了一个小实用程序并为其添加了一些调试UI:
private void backupDb() throws IOException {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "/data/com.yourcompany.yourapp/databases/yourapp.db";
String backupDBPath = "/yourapp_logs/yourapp.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (backupDB.exists())
backupDB.delete();
if (currentDB.exists()) {
makeLogsFolder();
copy(currentDB, backupDB);
}
dbFilePath = backupDB.getAbsolutePath();
}
}
private void makeLogsFolder() {
try {
File sdFolder = new File(Environment.getExternalStorageDirectory(), "/yourapp_logs/");
sdFolder.mkdirs();
}
catch (Exception e) {}
}
private void copy(File from, File to) throws FileNotFoundException, IOException {
FileChannel src = null;
FileChannel dst = null;
try {
src = new FileInputStream(from).getChannel();
dst = new FileOutputStream(to).getChannel();
dst.transferFrom(src, 0, src.size());
}
finally {
if (src != null)
src.close();
if (dst != null)
dst.close();
}
}
您的代码看起来很好,但我的Eclipse无法识别副本(currentDB,backupDB); 指令.您是否安装了一些额外的库(.jar)来使用复制方法? (2认同)