How to save string as html file in cache directory and retrieve that file from cache directory and show it in web view in android studio?

huangapple go评论107阅读模式
英文:

How to save string as html file in cache directory and retrieve that file from cache directory and show it in web view in android studio?

问题

如何将字符串保存为HTML文件、检索它并在WebView中加载它?

我遇到的情况是,当访问API时,我会获得响应作为字符串,该字符串的格式为HTML,如下所示:

<!DOCTYPE html>
<html>
    <head>
        <title> <!-- title bar --> </title>
        <!-- header for the website -->
    </head>
    <body>
       
        <!-- body section of the website -->
   
    </body>
</html>

我将此字符串保存为HTML文件,存储在缓存目录中,如下所示:

val htmlFile = File(
   mContext.cacheDir,
   System.currentTimeMillis().toString() + ".html"
)
val out = FileOutputStream(htmlFile)
val data: ByteArray = htmlString.toByteArray()
out.write(data)
out.close()

然后从htmlFile.absolutePath检索该文件路径,并在WebView中加载该路径。

这会显示"Web页不可用"的错误。

如何获取HTML文件的路径并在WebView中加载它?

注意:
当我将HTML文本保存为位于assets文件夹内的HTML文件,并将该文件加载为字符串时,"file:///android_asset/test.html"可以在WebView中正常工作。

以下是完整的代码:

/* htmlString 是从API响应中获取的HTML格式的字符串 */
val htmlFile = File(
    mContext.cacheDir,
    System.currentTimeMillis().toString() + ".html"
)
val out = FileOutputStream(htmlFile)
val data: ByteArray = htmlString.toByteArray()
out.write(data)
out.close()
val htmlFilePath = htmlFile.absolutePath // 将其加载到WebView中显示错误"Web页不可用"
requireActivity().apply {
    val intent = Intent(this, WebviewActivity::class.java)
    intent.putExtra("title", "Dummy Title")
    intent.putExtra("url", htmlFilePath)
    startActivity(intent)
}

请提供解决方案,谢谢。

英文:

How to save string as html file and retrieve it and load it in webview?

I am in the situation that while hitting the api I get the response as String which is in Html format Like this as String

&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt; &lt;!-- title bar --&gt; &lt;/title&gt;
        &lt;!-- header for the website --&gt;
    &lt;/head&gt;
    &lt;body&gt;
       
        &lt;!-- body section of the website --&gt;
   
    &lt;/body&gt;
&lt;/html&gt;

I save this string as html file inside the cache directory like this

   val htmlFile = File(
       mContext.cacheDir,
       System.currentTimeMillis().toString() + &quot;.html&quot;
   )
   val out = FileOutputStream(htmlFile)
   val data: ByteArray = htmlString.toByteArray()
   out.write(data)
   out.close()

And retrieve that file path from htmlFile.absolutePath and load that path in web view.

This shows the error that Webpage is not available.

How to get the path of the html file and load it in webview?

Note:
when I save the html text as html file inside the assets folder and load that file as string &quot;file:///android_asset/test.html&quot; works inside the webview.

Full Code is Added Below:

          /*htmlString is the string obtain from api response is in html format like 
            * &lt;!DOCTYPE html&gt;
                &lt;html&gt;
                    &lt;head&gt;
                        &lt;title&gt; &lt;!-- title bar --&gt; &lt;/title&gt;
                        &lt;!-- header for the website --&gt;
                    &lt;/head&gt;
                    &lt;body&gt;
                        &lt;!-- body section of the website --&gt;
                    &lt;/body&gt;
                &lt;/html&gt;
            */
            val htmlFile = File(
                mContext.cacheDir,
                System.currentTimeMillis().toString() + &quot;.html&quot;
            )
            val out = FileOutputStream(htmlFile)
            val data: ByteArray = htmlString.toByteArray()
            out.write(data)
            out.close()
            val htmlFilePath = htmlFile.absolutePath // Loading this in WebView Shows Error Webpage is not available
            requireActivity().apply {
                val intent= Intent(this, WebviewActivity::class.java)
                intent.putExtra(&quot;title&quot;, &quot;Dummy Title&quot;)
                intent.putExtra(&quot;url&quot;, htmlFilePath)
                startActivity(intent)
            }

Please provide the solution for my question, Thanks

答案1

得分: 1

终于找到了解决此问题的方法

webview.apply {
   settings.allowContentAccess = true
   settings.allowFileAccess = true
}

在 WebView 中添加这行代码后,可以成功加载缓存目录中的 HTML 文件。

问题不在于将文件保存在缓存目录中或检索它。问题在于缺少 WebView 中的这两行代码。

英文:

Finally got solution for this problem

webview.apply {
   settings.allowContentAccess = true
   settings.allowFileAccess = true
}

After adding this line in webview, html file from cache dir load successfully.

Problem not in the Saving File in cache directory or retrieve it. Its about missing these two lines in webview

huangapple
  • 本文由 发表于 2023年3月7日 21:44:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定