3

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>" 
2
  • Just write it as a file with html extension like "sample.html". For how to write file, you can read this post stackoverflow.com/questions/14376807/… Commented Jul 22, 2015 at 3:50
  • @Minhtdh doesn't work. I tried it. Commented Jul 22, 2015 at 5:53

4 Answers 4

5

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(); } } 
Sign up to request clarification or add additional context in comments.

3 Comments

Ok Its working now. I had forgotten to add permissions. However, the images are not displaying for some reason.
@Abhishek Singh Add this permission in menifest file <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>.
Already done. I got it working also but I am not seeing any images working. Probably the path of images is not properly added in html. Images don't show up. Everything else does.
1
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); 

Comments

0

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(); } 

Comments

0

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"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.