我的問題只是如何將字符串HTML保存爲內部存儲中的.html文件。請讓我知道如何做到這一點。將字符串保存爲html文件android
比方說串我是:
string html="<html><head><title>Title</title></head><body>This is random text.</body></html>"
我的問題只是如何將字符串HTML保存爲內部存儲中的.html文件。請讓我知道如何做到這一點。將字符串保存爲html文件android
比方說串我是:
string html="<html><head><title>Title</title></head><body>This is random text.</body></html>"
試用此。
private void saveHtmlFile() {
String path = Environment.getExternalStorageDirectory().getPath();
String fileName = DateFormat.format("dd_MM_yyyy_hh_mm_ss", System.currentTimeMillis()).toString();
fileName = fileName + ".html";
File file = new File(path, fileName);
String html = "<html><head><title>Title</title></head><body>This is random text.</body></html>";
try {
FileOutputStream out = new FileOutputStream(file);
byte[] data = html.getBytes();
out.write(data);
out.close();
Log.e(TAG, "File Save : " + file.getPath());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
好吧現在它的工作。我忘記了添加權限。但是,由於某種原因,圖像不顯示。 –
@Abhishek Singh在menifest文件
已經完成。我也得到了它的工作,但我沒有看到任何圖像的工作。可能圖像的路徑沒有正確添加到html中。圖像不顯示。其他一切都會。 –
試試這個:
public void writeHTML() throws IOException
{
String html="<html><head><title>Title</title></head><body>This is random text.</body></html>"
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("myfile.html", Context.MODE_PRIVATE));
outputStreamWriter.write(html);
outputStreamWriter.close();
}
不起作用。我嘗試過這個。 –
只要把它寫成一個像「sample.html」這樣的html擴展名的文件即可。對於如何寫文件,你可以閱讀這篇文章http://stackoverflow.com/questions/14376807/how-to-read-write-string-from-a-file-in-android – Minhtdh
@Minhtdh不起作用。我嘗試過這個。 –