2014-01-12 42 views
0

嗨,我有一個字符串,其中包含html和css,我想加載到一個WebView作爲一個文件。所以我想在內部存儲上創建一個目錄,我不想將它存儲在SD卡上,因爲我保存的文件包含重要信息。然後我想創建一個mFile.html而不是該目錄。然後最後一步是將文件返回加載到WebView中。有誰知道如何做到這一點?編寫/返回一個HTML字符串文件到內部文件目錄android

繼承人什麼我都試過到目前爲止

public static void writeFileToDirectory(Context activityContext, String writableString,String folderName, String fileName){ 


    File myDir = activityContext.getFilesDir(); 

    try { 
     File secondFile = new File(myDir + "/" + folderName + "/", fileName); 
     if (secondFile.getParentFile().mkdirs()) { 
      secondFile.createNewFile(); 
      FileOutputStream fos = new FileOutputStream(secondFile); 

      fos.write(writableString.getBytes()); 
      fos.flush(); 
      fos.close(); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    try { 
     File secondInputFile = new File(myDir + "/html/", fileName); 
     InputStream secondInputStream = new BufferedInputStream(new FileInputStream(secondInputFile)); 
     BufferedReader r = new BufferedReader(new InputStreamReader(secondInputStream)); 
     StringBuilder total = new StringBuilder(); 
     String line; 
     while ((line = r.readLine()) != null) { 
      total.append(line); 
     } 
     r.close(); 
     secondInputStream.close(); 
     Log.d("File", "File contents: " + total); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

回答

0
try this 

File myDir = activityContext.getFilesDir(); 

try { 
    File secondFile = new File(myDir + "/" + folderName); 
     if(!secondFile.exists()) 
     { 
      secondFile.mkdir(); 
     } 
     FileOutputStream fos = new FileOutputStream(secondFile.getAbsolutePath()+"/"+fileaName); 

     fos.write(writableString.getBytes()); 
     fos.flush(); 
     fos.close(); 
    } 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
相關問題