1. 程式人生 > >Android載入raw資料夾下的檔案

Android載入raw資料夾下的檔案

res/raw工程目錄下都可以放一些檔案,這些檔案將被打包到APK中應用使用。這些檔案不會被平臺編譯,而是作為可用的原始資源。
讀取res/raw下的檔案資源,通過以下方式獲取輸入流來進行寫操作
InputStream is =getResources().openRawResource(R.id.filename);
使用這裡面的從資源有兩種方法:
第一種,將檔案拷貝到data/data目錄下,以(以載入資料庫為例)
具體程式碼如下:

public class DBManager {
    private final int BUFFER_SIZE = 40000;
    public
static final String DB_NAME = "dbName.db"; // 儲存的資料庫檔名(raw資料夾下資料庫檔案的名稱) public static final String PACKAGE_NAME = "com.example.proName"; // sd卡資料庫在此位置下(修改為你專案所在的包名) public static final String DB_PATH = "/data" + Environment.getDataDirectory().getAbsolutePath() + "/" + PACKAGE_NAME; // 在手機裡存放資料庫的位置(/data/data/com.example.proName(你的專案的包名)/dictionarydanci.db(資料庫檔名稱))
private SQLiteDatabase database; private Context context; public DBManager(Context context) { this.context = context; } public SQLiteDatabase getDatabase() { return database; } public void setDatabase(SQLiteDatabase database) { this.database = database; } public
void openDatabase() { System.out.println(DB_PATH); System.out.println(DB_NAME); System.out.println(DB_PATH + "/" + DB_NAME+"!!!!!!!!!!"); this.database = this.openDatabase(DB_PATH + "/" + DB_NAME); } private SQLiteDatabase openDatabase(String dbfile) { try { if (!(new File(dbfile).exists())) { // 判斷資料庫檔案是否存在,若不存在則執行匯入,否則直接開啟資料庫 InputStream is = this.context.getResources().openRawResource( R.raw.dictionarydanci); // 欲匯入的資料庫 FileOutputStream fos = new FileOutputStream(dbfile); byte[] buffer = new byte[BUFFER_SIZE]; int count = 0; while ((count = is.read(buffer)) > 0) { fos.write(buffer, 0, count); } fos.close(); is.close(); } SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null); return db; } catch (FileNotFoundException e) { Log.e("Database", "沒有找到資料庫。"); e.printStackTrace(); } catch (IOException e) { Log.e("Database", "IO異常。"); e.printStackTrace(); } return null; } public void closeDatabase() { this.database.close(); } // ///////////////////////////////////// }

使用這種方法將raw檔案下的資料庫檔案複製到/data/data/com.example.proName(你的專案的包名)/dbName.db(資料庫檔名稱)資料夾(也可以是你想要的任何資料夾)下
之後就可以使用常用的資料庫dao類對資料庫進行相關的增刪改差操作
在dao類中,使用SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
DBManager.DB_PATH + “/” + DBManager.DB_NAME, null);
這條語句讀取已經存在的資料庫,之後使用database對資料庫進行增刪改差的操作
!!!:在使用之前注意判斷資料庫檔案是否存在,同時判斷資料庫檔案是否開啟
dao類中資料庫檔案使用之後要注意及時的釋放資源,關閉資料庫檔案和遊標結果集合
第二種:利用快取使用
raw下的檔案不同於assert下的,raw中的檔案會在R檔案中生成ID,R類會自動提供該id.提速檔案讀取。其原理就是讀的時候,先把檔案的一些資料讀到緩衝中。這樣的好處是如果讀的內容已經在緩衝中,就讀緩衝的資料。如果沒有,就讓緩衝先從檔案讀取資料,然後再從緩衝讀資料。
InputStream is = getResources().openRawResource(R.id.fileNameID) ;
//R.id.fileNameID為需要訪問的檔案對應的資源ID.接著我們就可以通過輸入流來讀取相應檔案的內容了。

 void readRawFile()
    {
        String content;
        Resources resources=this.getResources();
        InputStream is=null;
        try{
            is=resources.openRawResource(R.raw.hubin);
            byte buffer[]=new byte[is.available()];
            is.read(buffer);
            content=new String(buffer);
            Log.i(tag, "read:"+content);
        }
        catch(IOException e)
        {
            Log.e(tag, "write file",e);
        }
        finally
        {
            if(is!=null)
            {
                try{
                is.close();
                }catch(IOException e)
                {
                    Log.e(tag, "close file",e);
                }
            }
        }        
    }

實際上這兩種方法沒有本質的區別,歸根到底都是利用 InputStream is =getResources().openRawResource(R.id.filename); 進行讀取,只不過第一種是將檔案拷貝的apk安裝之後的檔案路徑下,可以在安裝時就進行,安裝之後就無需在raw檔案下進行讀取操作了。而第二種方式是將檔案載入到快取之中,隨用隨取。這樣的好處是如果讀的內容已經在緩衝中,就讀緩衝的資料如果沒有,就讓緩衝先從檔案讀取資料,然後再從緩衝讀資料。這樣的好處是減少對檔案的操作次數,從而達到提高效能的目的。壞處是要額外的記憶體來做緩衝區。而具體到底該使用那種方式,就得看實際訪問的檔案型別和大小了。