歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 往自己的APP加入文件

Android 往自己的APP加入文件

日期:2017/3/1 11:11:21   编辑:Linux編程

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. }
Copyright © Linux教程網 All Rights Reserved