在使用真機調試資料庫應用的時候,沒有Root過的手機,沒有辦法通過檔瀏覽器查看手機裡面的資料庫。沒有辦法,只能將資料庫複製到SD卡上面,然後再調試。代碼如下
01 package com.mm.util;
02   
03 import java.io.File;
04 import java.io.FileInputStream;
05 import java.io.FileNotFoundException;
06 import java.io.FileOutputStream;
07 import java.io.IOException;
08 import java.nio.channels.FileChannel;
09   
10 import android.content.Context;
11 import android.util.Log;
12   
13 public class Database2SD {
14       
15     public static void copyDatabase2SD(Context context,String databaseName){
16         String fileName="/data/data/"+context.getPackageName()+"/databases/"+databaseName;
17           
18         File file=new File("/data/data/"+context.getPackageName());
19         for(String path:file.list()){
20             Log.e("Database2SD",path);
21         }
22         File databaseFile=new File(fileName);
23           
24         try {
25             FileInputStream fis = new FileInputStream(databaseFile);
26             FileChannel inChannel=fis.getChannel();
27               
28             FileOutputStream fos=new FileOutputStream(new File("/sdcard/"+databaseName));
29             FileChannel outChannel=fos.getChannel();
30             outChannel.transferFrom(inChannel, 0, inChannel.size());
31               
32             fis.close();
33             fos.close();
34         } catch (FileNotFoundException e) {
35             // TODO Auto-generated catch block
36             e.printStackTrace();
37         } catch (IOException e) {
38             // TODO Auto-generated catch block
39             e.printStackTrace();
40         }
41           
42           
43     }
44 }
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()