但root過的device才能在Eclipse> DDMS> File Explorer> /data/data/<package_name>/databases/資料庫名.db ←找到.db檔
從電腦終端機執行adb shell想把.db檔pull出來, 又有「opendir failed, Permission denied」的問題←因為沒root
如果很不想root device,又想要看.db檔,該怎麼做呢?
這裡提供一個方法,在程式中寫段code把.db檔copy到指定目錄下
// 先建立過Cursor才會產生.db檔
public static boolean copyFile(){
try{
String sourceFile = "/data/data/com.tsots.PieFee/databases/PieFee.db";
String destFile = Environment.getExternalStorageDirectory() + "/PieFee/Log/PieFee.db";
//File file1 = new File(sourceFile);
//Log.d("522","file1存在嗎?"+file1.exists());
//File file2 = new File(destFile);
//Log.d("522","file2存在嗎?"+file2.exists());
File file = new File(destFile);
if(!file.exists()) {
Log.d("522","路徑之前不存在, 新建立");
file.getParentFile().mkdirs();
}
InputStream in = new FileInputStream(sourceFile);
OutputStream out = new FileOutputStream(destFile);
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
Log.d("522","save file["+destFile+"] ok from["+sourceFile+"]");
} catch(FileNotFoundException ex){
Log.d("522", "File Not Found Exception " + ex.toString());
return false;
} catch (IOException io){
return false;
}
return true;
}
那麼在Eclipse> DDMS> File Explorer> /storage/sdcard0/PieFee/Log/PieFee.db,就可以拿到這個.db檔囉!

沒有留言:
張貼留言