讀取Assets所存在的所有文件(遍歷每一個文件夾),並存入sdcard里面


private  void CopyAssets(String assetDir, String dir) {
        String[] files;
         try {
             //  獲得Assets一共有幾多文件
            files =  this.getResources().getAssets().list(assetDir);
        }  catch (IOException e1) {
             return;
        }
        File mWorkingPath =  new File(dir);
         //  如果文件路徑不存在
         if (!mWorkingPath.exists()) {
             //  創建文件夾
             if (!mWorkingPath.mkdirs()) {
                 //  文件夾創建不成功時調用
            }
        }

         for ( int i = 0; i < files.length; i++) {
             try {
                 //  獲得每個文件的名字
                String fileName = files[i];
                 //  根據路徑判斷是文件夾還是文件
                 if (!fileName.contains(".")) {
                     if (0 == assetDir.length()) {
                        CopyAssets(fileName, dir + fileName + "/");
                    }  else {
                        CopyAssets(assetDir + "/" + fileName, dir + "/"
                                + fileName + "/");
                    }
                     continue;
                }
                File outFile =  new File(mWorkingPath, fileName);
                 if (outFile.exists())
                    outFile.delete();
                InputStream in =  null;
                 if (0 != assetDir.length())
                    in = getAssets().open(assetDir + "/" + fileName);
                 else
                    in = getAssets().open(fileName);
                OutputStream out =  new FileOutputStream(outFile);

                 //  Transfer bytes from in to out
                 byte[] buf =  new  byte[1024];
                 int len;
                 while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                in.close();
                out.close();
            }  catch (FileNotFoundException e) {
                e.printStackTrace();
            }

             catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM