how open local html page in browser

前端 未结 5 1835
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 20:51

I am making an application in which i want to open a local html page in the browser. I am able to open google.com. But when i\'m opening local html file. But i am getting fo

5条回答
  •  一向
    一向 (楼主)
    2020-12-20 21:25

    To save the html string as file:

     public File saveStringToHtmlFile(String htmlString) {
            File htmlFile = null;
            try {
                // If the file does not exists, it is created.
                htmlFile = new File((ContextUtils.getAppContext()).getExternalFilesDir(null), "HTML_FILE_NAME.html");
                if (!htmlFile.exists()) {
                    htmlFile.createNewFile();
                }
    
                // Adds a line to the file
                BufferedWriter writer = new BufferedWriter(new FileWriter(htmlFile, false));
                writer.write(htmlString);
                writer.close();
            } catch (IOException e) {
                Log.e(TAG, "Unable to write to HTML_FILE_NAME.html file.");
            }
            return htmlFile;
        }
    

    To open the html file with browser:

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(htmlFile), "text/html");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    

提交回复
热议问题