My question simply is that how do I save string HTML as .html file in internal storage. Please let me know how to do this.
Lets say string I have is:
string html="<html><head><title>Title</title></head><body>This is random text.</body></html>"
My question simply is that how do I save string HTML as .html file in internal storage. Please let me know how to do this.
Lets say string I have is:
string html="<html><head><title>Title</title></head><body>This is random text.</body></html>"
Tryout This.
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();
}
}
You can convert with this
public static String saveHtml(Activity activity, String html) {
String filePath = "";
String fileName = "";
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&&
activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]
{Manifest.permission.WRITE_EXTERNAL_STORAGE}, ARE_Toolbar.REQ_VIDEO);
return "";
}
filePath = Environment.getExternalStorageDirectory() +
File.separator + "ARE" + File.separator;
File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdir();
}
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-
dd_hh_mm_ss");
String time = dateFormat.format(new Date());
fileName = time.concat(".html");
File file = new File(filePath + fileName);
if (!file.exists()) {
boolean isCreated = file.createNewFile();
if (!isCreated) {
com.chinalwb.are.Util.toast(activity, "Cannot create file
at: " + filePath);
return "";
}
}
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(html);
fileWriter.close();
com.chinalwb.are.Util.toast(activity, fileName + " has been saved
at " + filePath);
} catch (IOException e) {
e.printStackTrace();
com.chinalwb.are.Util.toast(activity, "Run into error: " +
e.getMessage());
}
return filePath + fileName;
}
**Call this method as -**
saveHtml(this, html);
Try This:
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();
}
If it helps somebodey, there is a simple solution. Youse saveWebarchive.
In Kotlin:
webView.loadUrl(URL);
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
return false
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
webView.saveWebArchive("$filesDir/myPage.mht")
}
}
And you can simple load it
webView.loadUrl("$filesDir/myPage.mht");