標籤:com code cti 移動 rect 資料庫 資料 src environ
在Android開發中,如果用到資料庫來儲存資料,那麼難免就要查看資料庫中的內容,可是對於未root的真機來說,查看資料庫就不是那麼容易了,如果僅僅為了查看資料庫再把手機root了,有點得不償失,所以下面的就提供了一中不用root也可查看資料庫的方法,供需要的朋友參考。
1. 進入adb所在的目錄,開啟cmd視窗,輸入`adb shell`
2. 繼續輸入`run-as 應用的包名`
3. 然後輸入 `cd databases`
4. 最後輸入`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()+"/copy.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:自己開發了一個製作二維碼的小應用,有興趣的朋友可以試一試~ ^_^ 創意二維碼製作
Android開發中查看未root真機的app資料庫