Android開發中檢視未root真機的app資料庫
阿新 • • 發佈:2019-01-04
在Android開發中,如果用到資料庫來儲存資料,那麼難免就要檢視資料庫中的內容,可是對於未root的真機來說,檢視資料庫就不是那麼容易了,如果僅僅為了檢視資料庫再把手機root了,有點得不償失,所以下面的就提供了一中不用root也可檢視資料庫的方法,供需要的朋友參考。
- 進入adb所在的目錄,開啟cmd視窗,輸入
adb shell
- 繼續輸入
run-as 應用的包名
- 然後輸入
cd databases
- 最後輸入
ls
檢視資料庫的名字
截圖如下:
如圖,我的資料庫名字為 app.db,然後利用以下程式碼把資料庫檔案移動到我們可以訪問的資料夾下面
private void getDatabaseFiles(){
//找到檔案的路徑 /data/data/包名/databases/資料庫名稱
File dbFile = new File(Environment.getDataDirectory().getAbsolutePath()+"/data/"+getPackageName()+"/databases/app.db");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//將資料庫檔案複製到sd卡的根目錄
fis = new FileInputStream(dbFile);
fos = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/app_test.db");
int len = 0;
byte[] buffer = new byte[2048];
while(-1!=(len=fis.read(buffer))){
fos.write(buffer, 0 , len);
}
fos.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
//完成之後關閉輸入輸出流
try {
if (fos != null) fos.close();
if (fis != null) fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
然後下一個可以檢視資料庫檔案的工具,就可以檢視資料庫的內容了。希望可以幫到你~
PS:開發了一個製作個性二維碼的應用,有興趣的朋友可以試一試~ 創意二維碼製作