Android 往自己的APP加入文件


Android 往自己的APP加入文件,首先把自己需要安装的文件放在工程里的assert目录下。然后代码如下;

  1.   /** @author  xuxin 
  2.     *down load tht simotun file 
  3.     */   
  4.     private void loadSimotunFile(){  
  5.         if(!isFileExist(mexeFilePath))  
  6.          try {  
  7.             InputStream in=getAssets().open("simotun") ;  
  8.             saveToFile(mexeFilePath,in);  
  9.         } catch (IOException e1) {  
  10.             e1.printStackTrace();  
  11.         }     
  12.     }   
 
  1. /** @author  xuxin 
  2. * get inputStream,and write it to a new file 
  3. */   
  4.    public void saveToFile(String fileName, InputStream in) throws IOException {   
  5.        FileOutputStream fos = null;      
  6.        BufferedInputStream bis = null;         
  7.        int BUFFER_SIZE = 1024;   
  8.        byte[] buf = new byte[BUFFER_SIZE];      
  9.        int size = 0;       
  10.        bis = new BufferedInputStream(in);      
  11.        fos = new FileOutputStream(fileName);        
  12.        while ( (size = bis.read(buf)) != -1)       
  13.          fos.write(buf, 0, size);                  
  14.        fos.close();      
  15.        bis.close();         
  16.      }   

相关内容