天天看點

/res/raw & /assets 攜帶資源

apk攜帶資源之raw & assets

文章分類:移動開發 wo-07 10-14 dfeixtay or

/res/raw & /assets 攜帶資源

    在android開發中,總有一些資源是想要随着安裝包apk攜帶的,這些資源如資料庫檔案,文本,mp3等等。最早的做法是,在prebulid過程 中,修改mk檔案,将指定檔案提前拷貝到系統某一檔案夾目錄下。這樣的做法,既不科學也不美觀還不安全,處于對代碼的潔癖,我終于在不太忙的時候把這些資 源檔案帶在自己的apk下。

    之前說過沒,由于提升速度,從文本解析調整為資料庫查詢。在攜帶檔案時,這兩種檔案本沒有不同,隻是這裡要講一點inputstream的時候,二進制文 件的db檔案,和純文字檔案txt的方式可以不同,原因好像是,純文字是按照unicode編碼的,是16位16位的傳的,二進制檔案是8位傳的。又想到 之前ftp傳輸的時候,也是寫的二進制傳輸。

    言歸正傳,無論是raw檔案夾還是assets檔案夾,都是在生成apk的時候不編譯而直接攜帶在apk的壓縮包中的,這可以打開apk檢驗。這想必也是raw的得名。

    提取的方法都是從inputstream轉,轉成什麼形式的,要看對inputstream的操作,下篇也許會講。具體的過程是:

Java代碼

  1. private   void  getRaw(){  
  2.         File target = new  File( "/data/data/com.android." );  
  3. //        InputStream in = this.getResources().openRawResource(R.raw.weather_db);   
  4. //        try {   
  5. //        } catch (IOException e1) {   
  6. //            // TODO Auto-generated catch block   
  7. //            e1.printStackTrace();   
  8. //        }   
  9.         InputStream in = null ;  
  10.         OutputStream out = null ;      
  11.         BufferedInputStream bin = null ;  
  12.         BufferedOutputStream bout = null ;  
  13.         try {  
  14.             //   
  15.             int  xx =  1 ; //R.raw.parse_weather_db_aa;   
  16.             xx+=1 ;  
  17.             try {  
  18.                 in = getResources().openRawResource(xx);  
  19.             }catch (Exception e){  
  20.                 e.printStackTrace();  
  21.             }  
  22.             out = new  FileOutputStream(target);  
  23.             bin = new  BufferedInputStream(in);  
  24.             bout = new  BufferedOutputStream(out);  
  25.             byte [] b =  new   byte [ 1024 ];  
  26.             int  len = bin.read(b);  
  27.             while  (len != - 1 ){  
  28.                 bout.write(b, 0 , len);  
  29.                 len = bin.read(b);  
  30.             }  
  31. //            exec("chmod 777 "+target.getAbsolutePath());   
  32.         }  
  33.         catch  (FileNotFoundException e){  
  34.             e.printStackTrace();  
  35.         }  
  36.         catch  (IOException e)  
  37.         {  
  38.             e.printStackTrace();  
  39.         }  
  40.         finally   
  41.         {  
  42.             try {  
  43.                 if  (bin !=  null ){  
  44.                     bin.close();  
  45.                 }  
  46.                 if  (bout !=  null ){  
  47.                     bout.close();  
  48.                 }  
  49.             }  
  50.             catch  (IOException e){  
  51.                 e.printStackTrace();  
  52.             }  
  53.         }  
  54.     }  
  55.     private   void  getAsset(){  
  56. //        if(WeatherWidget.loadRunning(this, "FirstSetup")){   
  57.         if ( true ){  
  58.             WeatherWidget.saveRunning(this ,  false ,  "FirstSetup" );  
  59.             File databs = new  File( "/data/data/com.android." );  
  60.             if (!databs.exists()){  
  61.                 databs.mkdir();  
  62.             }  
  63.             InputStream in = null ;  
  64.             OutputStream out = null ;      
  65.             BufferedInputStream bin = null ;  
  66.             BufferedOutputStream bout = null ;  
  67.             AssetManager am = getAssets();  
  68.             String asName = "parse_weather_db_aa" ;  
  69.             while ( true ){  
  70.                 try {  
  71.                     in = am.open(asName);  
  72.                     out = new  FileOutputStream(databs.getPath()+ "/weather_db" );  
  73.                     asName = nextAsset(asName);  
  74.                     bin = new  BufferedInputStream(in);  
  75.                     bout = new  BufferedOutputStream(out);  
  76.                     byte [] b =  new   byte [ 8192 ];  
  77.                     int  len = bin.read();  
  78.                     while  (len != - 1 ){  
  79.                         bout.write(b, 0 , len);  
  80.                         len = bin.read(b);  
  81.                     }    
  82.                     continue ;  
  83.                 }  
  84.                 catch  (FileNotFoundException e){  
  85.                     e.printStackTrace();  
  86.                     try {  
  87.                         if  (bin !=  null ){  
  88.                             bin.close();  
  89.                         }  
  90.                         if  (bout !=  null ){   
  91.                             bout.close();  
  92.                         }  
  93.                         break ;  
  94.                     }  
  95.                     catch  (IOException ee){  
  96.                         ee.printStackTrace();  
  97.                     }  
  98.                     break ;  
  99.                 }  
  100.                 catch  (IOException e){  
  101.                     e.printStackTrace();  
  102.                     try {  
  103.                         if  (bin !=  null ){  
  104.                             bin.close();  
  105.                         }  
  106.                         if  (bout !=  null ){  
  107.                             bout.close();  
  108.                         }  
  109.                         break ;  
  110.                     }  
  111.                     catch  (IOException eee){  
  112.                         eee.printStackTrace();  
  113.                     }  
  114.                     break ;  
  115.                 }  
  116.             }  
  117.         }  
  118.     }  
private void getRaw(){
        File target = new File("/data/data/com.android.");

//        InputStream in = this.getResources().openRawResource(R.raw.weather_db);
//        try {
//        } catch (IOException e1) {
//            // TODO Auto-generated catch block
//            e1.printStackTrace();
//        }
        InputStream in = null;
        OutputStream out = null;    
          
        BufferedInputStream bin = null;
        BufferedOutputStream bout = null;
        try{
            //
            int xx = 1;//R.raw.parse_weather_db_aa;
            xx+=1;
            try{
                in = getResources().openRawResource(xx);
            }catch(Exception e){
                e.printStackTrace();
            }

            out = new FileOutputStream(target);
            bin = new BufferedInputStream(in);
            bout = new BufferedOutputStream(out);

            byte[] b = new byte[1024];
            int len = bin.read(b);

            while (len != -1){
                bout.write(b, 0, len);
                len = bin.read(b);
            }
//            exec("chmod 777 "+target.getAbsolutePath());
        }
        catch (FileNotFoundException e){

            e.printStackTrace();
        }
        catch (IOException e)
        {

            e.printStackTrace();
        }
        finally
        {
            try{
                if (bin != null){
                    bin.close();
                }
                if (bout != null){
                    bout.close();
                }
            }
            catch (IOException e){
                e.printStackTrace();
            }
        }
    }
    
    private void getAsset(){
//        if(WeatherWidget.loadRunning(this, "FirstSetup")){
        if(true){

            WeatherWidget.saveRunning(this, false, "FirstSetup");
            
            File databs = new File("/data/data/com.android.");
            if(!databs.exists()){
                
                databs.mkdir();
            }
            
            InputStream in = null;
            OutputStream out = null;    
              
            BufferedInputStream bin = null;
            BufferedOutputStream bout = null;
            
            AssetManager am = getAssets();
            String asName = "parse_weather_db_aa";
            

            while(true){
                
                try{
                    
                    in = am.open(asName);
                    out = new FileOutputStream(databs.getPath()+"/weather_db");
                    asName = nextAsset(asName);
                    
                    
                    bin = new BufferedInputStream(in);
                    bout = new BufferedOutputStream(out);
                    
                    byte[] b = new byte[8192];
                    int len = bin.read();
                    while (len != -1){
                        bout.write(b, 0, len);
                        len = bin.read(b);
                    }  
                    continue;
                }
                catch (FileNotFoundException e){
                    e.printStackTrace();
                    try{
                        if (bin != null){
                            bin.close();
                        }
                        if (bout != null){ 
                            bout.close();
                        }
                        break;
                    }
                    catch (IOException ee){
                        ee.printStackTrace();
                    }
                    break;
                }
                catch (IOException e){
                    e.printStackTrace();
                    try{
                        if (bin != null){
                            bin.close();
                        }
                        if (bout != null){
                            bout.close();
                        }
                        break;
                    }
                    catch (IOException eee){
                        eee.printStackTrace();
                    }
                    break;
                }
            }
        }
    }
           

    代碼比較亂,有些東西講一遍不如需要的時候直接回來看一眼代碼,去僞存真吧。

    最後,講raw & assets資源比較重要的一點,就是檔案的大小限制,單個檔案的大小不可以大于1M,有人說這是android的bug,呵呵,解決的方法是:

    将檔案split為小于1M的檔案,在讀取的時候outputstream不要close,而是合并寫這些檔案,最後就得到原始檔案。分割可以用ubuntu指令,

  • split -b[byte] 512k [source] [prefix]

也可以用windows下檔案分割器,或者直接在android中制作,代碼inputstream的時候限制大小,分割存儲,我沒有實踐過,隻是看到有人是這麼寫的。

以下是原文,需要時搜尋關鍵字,網址沒了。

引用

漢語詞典開發-assets,raw的InputStream資料流操作(檔案分割合并)

文章分類:移動開發

檔案移動

一. 在應用中由于種種原因需要将一些外部檔案放在 assets 或者 raw 檔案夾内,以便進一步使用。這兩個檔案夾有以下的差別和聯系:

1. 都是以資料流的形式進行讀取,進而導緻 Java 中其他的一些讀取方式不能很好的作用在這些檔案上面,例如 RandomAccessFile 、 FileReader 等之類的類。如果由于需要需要使用基于檔案的類,則可以根據資料流建立建立臨時檔案( File.createTempFile )當做一個折中的辦法。這是一種方法,下面将會介紹另一種方法

2. raw 檔案夾中的檔案不能包含有目錄結構并且每個檔案會映射到一個 id ,而 assets 檔案夾可以有目錄結構。對于對檔案名敏感的程式則使用 raw 進行外部檔案存儲較為友善,而對于較依賴目錄結構的檔案則使用 assets 存儲

3. 這兩個檔案夾中的檔案都不能太大,官方資料是小于 1M 。這點需要時刻記住,因為産生的問題十分隐蔽,在程式中可以找到該檔案也可以産生 inputStream 但是在讀取時會抛出 IOException 異常。這中大檔案需要先分割在進行讀取

4. 這兩個檔案夾對檔案名稱大小寫敏感,命名是盡量用小寫,并且在分割合并後也要注意檔案名稱,否則程式會認為它們是不同的檔案,但是在建立時會覆寫掉先前的檔案(這點太隐蔽了, ~~~~(>_<)~~~~ )

以下是分割資料的代碼:

Java代碼

1. public static void CutFilesInSizeParts(InputStream fis,   

2.         String OutputFileName, int MaxPartSize) {   

3.     try {   

4.            

5.         int TotalLength = fis.available();   

6.         byte[] buffer = new byte[TotalLength + 1];   

7.         int len = fis.read(buffer);   

8.   

9.         int nbPart = len / MaxPartSize + 1;   

10.         int CurPos = 0;   

11.   

12.         for (int i = 0; i < nbPart; i++) {   

13.             int PartLen = MaxPartSize;   

14.             if (CurPos + PartLen >= len)   

15.                 PartLen = len - CurPos;   

16.             String outRealFileName = OutputFileName + (i + 1);   

17.             FileOutputStream fos = new FileOutputStream(outRealFileName);   

18.             fos.write(buffer, CurPos, PartLen);   

19.             CurPos += PartLen;   

20.         }   

21.     } catch (IOException e) {   

22.         e.printStackTrace();   

23.     }   

24. }  

public static void CutFilesInSizeParts(InputStream fis,

String OutputFileName, int MaxPartSize) {

try {

int TotalLength = fis.available();

byte[] buffer = new byte[TotalLength + 1];

int len = fis.read(buffer);

int nbPart = len / MaxPartSize + 1;

int CurPos = 0;

for (int i = 0; i < nbPart; i++) {

int PartLen = MaxPartSize;

if (CurPos + PartLen >= len)

PartLen = len - CurPos;

String outRealFileName = OutputFileName + (i + 1);

FileOutputStream fos = new FileOutputStream(outRealFileName);

fos.write(buffer, CurPos, PartLen);

CurPos += PartLen;

}

} catch (IOException e) {

e.printStackTrace();

}

}

二,正是以上兩個檔案夾隻能産生 InputStream 資料流,當程式有别的需要時會顯的無能為力。例如在外面建立了一個 sqlite 的 db 檔案,該檔案需要内置到 apk 中隻能放入這兩個檔案夾中,可是在使用時可要根據需要放置到 sdcard 中或者 database 中去。是以需要對讀取檔案并在相應位置生成目的檔案,按照自己需要的方式進行讀取,這也提供了第二種方法。這一步需要注意一下幾點:

1. 檔案的權限,否則可能會被别的應用使用

2. 若是要對分割後的檔案進行合并,則要注意檔案順序

這裡附上合并資料并拷貝的代碼

Java代碼

1. //合并并拷貝資料   

2.     public static void CreateFromRawDbFiles(File[] filelist,   

3.             FileOutputStream Fos) {   

4.   

5.         try {   

6.             for (File file : filelist) {   

7.                 InputStream inputFile = new FileInputStream(file);   

8.                 int TotalLength = 0;   

9.                 try {   

10.                     TotalLength = inputFile.available();   

11.                 } catch (IOException e) {   

12.                 }   

13.                 // Reading and writing the file Method 1 :   

14.                 byte[] buffer = new byte[TotalLength];   

15.                 int len = 0;   

16.                 try {   

17.                     len = inputFile.read(buffer);   

18.                 } catch (IOException e) {   

19.                 }   

20.                 Fos.write(buffer,0,len);   

21.                 inputFile.close();   

22.             }   

23.             Fos.close();   

24.         } catch (IOException e) {   

25.         }   

26.     }