【问题标题】:How to write html file to local storage in android and then read it?如何将html文件写入android的本地存储然后读取它?
【发布时间】:2015-11-30 05:09:56
【问题描述】:

我有一个 html 字符串:

String htmlString = "<h1>Hello World</h1><p>DaParagraph</p>"

我想将它作为 file.html 保存到内部/外部存储中,然后读取它。我该怎么办?

【问题讨论】:

标签: android


【解决方案1】:

另一种简单的选择。

您应该将该 htmlstring 作为字符串存储到 SQLite 数据库或 SharedPreference。

并在 webview 中将该字符串加载为 html

String htmlString = "<h1>Hello World</h1><p>DaParagraph</p>";
                webview.getSettings().setJavaScriptEnabled(true);
                webview.loadDataWithBaseURL("", htmlString, "text/html", "UTF-8","");

【讨论】:

    【解决方案2】:

    1.请将您的 html 保存在共享中,它是来自服务器的字符串响应 像这样的偏好:

    SharedPreferences preferences= PreferenceManager.getDefaultSharedPreferences(context);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("HTML", "your html string");
            editor.commit();  
    

    2。在任何你想使用的地方获取这个字符串:

    SharedPreferences preferences= PreferenceManager.getDefaultSharedPreferences(context); 
    String htmlString=preferences.getString("HTML", null);
    

    3.将字符串转换为 html:

    Html.fromHtml((String) htmlString).toString();
    

    编码愉快。!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-18
      • 2013-12-02
      相关资源
      最近更新 更多