2015-07-01 26 views
0

我需要在我的Android項目中放置一個文件夾,我可以使用File myDir = new File(path-to-dir);進行訪問,但似乎找不到辦法做到這一點。我曾嘗試將它放在assets文件夾中,但我需要訪問我的文件夾本身,因此我可以在File對象中對其執行操作。任何幫助,將不勝感激。Android項目中的自定義文件夾

+1

您需要將目錄及其內容從'assets /'複製到本地文件系統。資產不是設備上的文件。 – CommonsWare

+0

好主意,解決了謝謝 –

回答

0

步驟1:創建一個文件夾插件資產文件夾,還有把你的文件..

第2步:假設你有資產文件夾中的文件夾名爲MyFolder文件,試試這個代碼

ReadFromfile("/myfolder/myfile.txt", mContext); 

而且功能

public String ReadFromfile(String fileName, Context context) { 
    StringBuilder returnString = new StringBuilder(); 
    InputStream fIn = null; 
    InputStreamReader isr = null; 
    BufferedReader input = null; 
    try { 
     fIn = context.getResources().getAssets() 
       .open(fileName, Context.MODE_WORLD_READABLE); 
     isr = new InputStreamReader(fIn); 
     input = new BufferedReader(isr); 
     String line = ""; 
     while ((line = input.readLine()) != null) { 
      returnString.append(line); 
     } 
    } catch (Exception e) { 
     e.getMessage(); 
    } finally { 
     try { 
      if (isr != null) 
       isr.close(); 
      if (fIn != null) 
       fIn.close(); 
      if (input != null) 
       input.close(); 
     } catch (Exception e2) { 
      e2.getMessage(); 
     } 
    } 
    return returnString.toString(); 
} 

SO Question

相關問題