Android 往自己的APP加入文件,首先把自己需要安裝的文件放在工程裡的assert目錄下。然後代碼如下;
- /** @author xuxin
- *down load tht simotun file
- */
- private void loadSimotunFile(){
- if(!isFileExist(mexeFilePath))
- try {
- InputStream in=getAssets().open("simotun") ;
- saveToFile(mexeFilePath,in);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- /** @author xuxin
- * get inputStream,and write it to a new file
- */
- public void saveToFile(String fileName, InputStream in) throws IOException {
- FileOutputStream fos = null;
- BufferedInputStream bis = null;
- int BUFFER_SIZE = 1024;
- byte[] buf = new byte[BUFFER_SIZE];
- int size = 0;
- bis = new BufferedInputStream(in);
- fos = new FileOutputStream(fileName);
- while ( (size = bis.read(buf)) != -1)
- fos.write(buf, 0, size);
- fos.close();
- bis.close();
- }