Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save String as html file android

Tags:

html

android

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>"
like image 629
Abhishek Singh Avatar asked Apr 28 '26 08:04

Abhishek Singh


1 Answers

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();
            }
        }
like image 51
Piyush Malaviya Avatar answered Apr 29 '26 22:04

Piyush Malaviya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!