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>"
Abhishek Singh
  • 415
  • 1
  • 12
  • 24
  • Just write it as a file with html extension like "sample.html". For how to write file, you can read this post http://stackoverflow.com/questions/14376807/how-to-read-write-string-from-a-file-in-android – justHooman Jul 22 '15 at 03:50
  • @Minhtdh doesn't work. I tried it. – Abhishek Singh Jul 22 '15 at 05:53

4 Answers4

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();
            }
        }
Piyush Malaviya
  • 1,091
  • 13
  • 17
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);
being_sohelkhan
  • 141
  • 2
  • 11
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();
    }
Amy
  • 4,034
  • 1
  • 20
  • 34
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");