Android程式函數 將assets檔案夾下的檔案複製到手機的sd卡中(包括子檔案夾),androidassets

來源:互聯網
上載者:User

Android程式函數 將assets檔案夾下的檔案複製到手機的sd卡中(包括子檔案夾),androidassets

最近在做個功能是將asset檔案夾下的所有檔案(包括子檔案)全部拷貝出來到指定目錄下。所用的方法無非是用AssetManager。但是這裡 有個問題是也要講子檔案夾和子檔案都要拷貝出來。到網上Google了下,也到baidu搜尋了下,發現了很多類似問題。但好像都有問題。顯然只能做到將asset直接目錄下的檔案拷貝出來,但子檔案夾拷貝不出來,而且,碰到檔案夾,會拋異常。無奈自己只好動手寫了個。如下:

 private void CopyAssets(String assetDir,String dir) {
            String[] files;    
             try    
             {    
                 files = this.getResources().getAssets().list(assetDir);    
             }    
             catch (IOException e1)    
             {    
                 return;    
             }    
             File mWorkingPath = new File(dir);
             //if this directory does not exists, make one. 
             if(!mWorkingPath.exists())    
             {    
                 if(!mWorkingPath.mkdirs())    
                 {    

                 }    
             }    

             for(int i = 0; i < files.length; i++)    
             {    
                 try    
                 {    
                     String fileName = files[i]; 
                     //we make sure file name not contains '.' to be a folder. 
                     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();    
                 }         
            }

這裡主要用到了遞迴,其他倒是沒有什麼。倒是這裡還有個存在的問題:如何判斷路徑的是個檔案夾還是檔案,我目前使用的方法就是判斷有沒有尾碼,這個可能存在問題。

(轉:http://www.cnblogs.com/ctou45/archive/2011/06/01/2065460.html)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.