1. 程式人生 > >Android從Assets複製檔案到本地

Android從Assets複製檔案到本地

/***從asset複製檔案到記憶體****/
    private void copyByAssetsApk() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("==============從asset複製檔案到記憶體==============copyAssets============================.");
                    String newPath = FileUtils.DIR_apk;
                    File files = new
File(newPath); if (!files.exists()) { files.mkdirs(); } File file = new File(files, "Server.apk"); InputStream is = null; try { AssetManager manager = getAssets(); if
(manager == null) return; is = manager.open("Server.apk"); } catch (Exception e) { e.printStackTrace(); } if (is == null) return; FileOutputStream fos = new FileOutputStream(file); byte
[] buffer = new byte[1024]; int byteCount = 0; while ((byteCount = is.read(buffer)) != -1) {// 迴圈從輸入流讀取 // buffer位元組 fos.write(buffer, 0, byteCount);// 將讀取的輸入流寫入到輸出流 } fos.flush();// 重新整理緩衝區 is.close(); fos.close(); System.out.println("==============從asset複製檔案到記憶體==============copyAssets success============================."); UpdateUtils.getIntance().installServerApk(file); } catch (Exception e) { System.out.println("==============從asset複製檔案到記憶體==============copyAssets error============================."); e.printStackTrace(); } } }).start(); }