上下文:
我的应用程序有时会显示“Sheets”,这些是包含文本和图像的html文件:
然后我使用
问题:
我的应用程序允许用户下载这些表格以便稍后离线查看。因此,我下载了html,然后下载了图像到我的本地私有目录(
我的问题是:我应该将什么作为我的 WebView 的基本 URL,以便我获得预期的行为(即显示已下载的图像)。
我尝试过 "Context.getFileDir().getAbsolutePath()"、"content://com.android.htmlfileprovider" 等。
我应该做些什么不同的吗?
非常感谢,
这个很好用:
我的基础URL:
我的应用程序有时会显示“Sheets”,这些是包含文本和图像的html文件:
<p>image description </p>
<p><img src="/api/medias/get/videos/56afad6447eba61c8099544b?thumbnail=true&company=4f86d0a9d655ab9794f5d1d2&fullSizedCover=true"
alt=""
data-id="56afad6447eba61c8099544b"
data-type="video" data-width="640" data-height="1136" /></p>
然后我使用
body.loadDataWithBaseURL(<我的api url>, body, "text/html; charset=UTF-8", null, null);
。我认为这并不相关,但我还是说一下,我有一个包含css和javascript的模板body。js脚本检测图像点击并通过JavascriptInterface
将"data-id"传输给android方法。在这种情况下,它会打开视频播放器并播放视频。问题:
我的应用程序允许用户下载这些表格以便稍后离线查看。因此,我下载了html,然后下载了图像到我的本地私有目录(
Context.getFileDir()
),并更改了html的src,以将“缩略图”源设置为我下载的图像。<p>video</p>
<p><img src="69c623955ecb5bd414f908cd383f3809.jpg"
alt=""
data-id="56afad6447eba61c8099544b"
data-type="video" data-width="640" data-height="360" /></p>
我的问题是:我应该将什么作为我的 WebView 的基本 URL,以便我获得预期的行为(即显示已下载的图像)。
我尝试过 "Context.getFileDir().getAbsolutePath()"、"content://com.android.htmlfileprovider" 等。
我应该做些什么不同的吗?
非常感谢,
这个很好用:
Picasso.with(iv.getContext()).load(new File(getContext().getFilesDir() + "/" + "69c623955ecb5bd414f908cd383f3809.jpg")).into(iv);
html:<p>video</p>
<p><img src="69c623955ecb5bd414f908cd383f3809.jpg" alt="" data-id="56afad6447eba61c8099544b" data-type="video" data-width="640" data-height="360" /></p>
我的基础URL:
baseUrl = Uri.fromFile(getContext().getFilesDir()).toString();
最后:
webview.loadDataWithBaseURL(baseUrl, body, "text/html; charset=UTF-8", null, null);
file:///data/user/0/<my package>/files
,而new File(Context.getFilesDir()+"/"+"69c623955ecb5bd414f908cd383f3809.jpg").exists()==true
。 - Renaud Favierbody.getSettings().setAllowFileAccess(true);
body.getSettings().setAllowContentAccess(true);
body.getSettings().setAllowFileAccessFromFileURLs(true);
body.getSettings().setAllowUniversalAccessFromFileURLs(true);