android中加载assets中的资源文件


1.直接读取inputstream流

 

[java]  view plain copy print ?
  1. AssetManager assetManager = getAssets();//獲取其輸入流 然後直接讀取這個流  
  2. InputStream inputStream = assetManager.open("1.txt");    

 

 

2.複製asset指定文件到制定地方

 

[java]  view plain copy print ?
  1. /** 
  2.      * Copy assets util 
  3.      * @param path The dictionary path 
  4.      * @param filename  The filename 
  5.      * @param context  Your context 
  6.      */  
  7.     public void copyAssets(String path, String filename,Context context) {  
  8.         try {  
  9.   
  10.             File fpath = new File(PATH);  
  11.             if (!fpath.isDirectory()) {  
  12.                 fpath.mkdirs();  
  13.             }  
  14.             AssetManager assetManager = context.getAssets();  
  15.             InputStream inputStream = assetManager.open(filename);  
  16.             FileOutputStream fileOutputStream = new FileOutputStream(PATH  
  17.                     + filename);  
  18.             byte[] buffer = new byte[1024];  
  19.             int read;  
  20.             while ((read = inputStream.read(buffer)) != -1) {  
  21.                 fileOutputStream.write(buffer, 0, read);  
  22.             }  
  23.             inputStream.close();  
  24.             inputStream = null;  
  25.             fileOutputStream.flush();  
  26.             fileOutputStream.close();  
  27.             fileOutputStream = null;  
  28.   
  29.         } catch (Exception e) {  
  30.             // TODO: handle exception  
  31.         }  
  32.     }  

3.複製assets下所有文件到指定地方

 

 

[java]  view plain copy print ?
  1. private static void copyAssets(Context context) {//copy Assets的方法    
  2.             
  3.             AssetManager assetManager = context.getAssets();    
  4.             String[] files = null;    
  5.             try {    
  6.                 files = assetManager.list("");    
  7.             } catch (IOException e) {    
  8.         
  9.             }    
  10.             for (int i = 0; i < files.length; i++) {    
  11.                 InputStream in = null;    
  12.                 OutputStream out = null;    
  13.                 try {    
  14.                     if (!(new File(Tips.DATA_PATH + files[i])).exists()) {    
  15.                         in = assetManager.open(files[i]);    
  16.                         out = new FileOutputStream(Tips.DATA_PATH + files[i]);    
  17.                         Tips.copyFile(in, out);    
  18.                         in.close();    
  19.                         in = null;    
  20.                         out.flush();    
  21.                         out.close();    
  22.                         out = null;    
  23.                     }    
  24.                 } catch (Exception e) {    
  25.                 }    
  26.             }    
  27.         }    
  28.         
  29.         public static void copyFile(InputStream in, OutputStream out)    
  30.                 throws IOException {    
  31.             byte[] buffer = new byte[1024];//做了个缓冲流    
  32.             int read;    
  33.             while ((read = in.read(buffer)) != -1) {    
  34.                 out.write(buffer, 0, read);    
  35.             }    
  36.         }    

猜你喜欢

转载自sfshine.iteye.com/blog/1728345