首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Android WebView中加载本地镜像

如何在Android WebView中加载本地镜像
EN

Stack Overflow用户
提问于 2017-02-16 14:12:47
回答 4查看 11.9K关注 0票数 9

我正在尝试加载一个存储在数据库中的html字符串,其中包含一个图像到一个WebView中。图像存储在内部存储器中。我给出了一个html string.But的参考,它不工作。有帮助吗?

代码语言:javascript
运行
复制
String content="<p>Can we have a rotational" +
    " symmetry of order more than 1 whose angle of rotation is&nbsp;</p>\n" +
    "\n <p>(i)&nbsp;<img alt=\"45\\degree\" src=\"file:///storage/emulated/01484890695248.jpg\" /></p>\n" +
    "\n<p>(ii)&nbsp;<img alt=\"35\\degree\" src=\"file:///storage/emulated/01484890697301.jpg\" /></p>";

WebView00.loadDataWithBaseURL("", content, "text/html","UTF-8", "");
WebView00.getSettings();
EN

Stack Overflow用户

发布于 2021-01-04 22:04:29

在最新的安卓版本中,我们无法从存储中访问assets、文件等资源。https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader是继续进行的最佳指南。WebViewAssetLoader及其内部类有助于访问它们。

您可以查看以下示例代码。

代码语言:javascript
运行
复制
    final WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
            .addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler(this)) //****for assets****
            .addPathHandler("/images/", new WebViewAssetLoader.InternalStoragePathHandler(context, getFilesDir()))//****for files****
            .build();

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view,
                                                          WebResourceRequest request) {
            return assetLoader.shouldInterceptRequest(request.getUrl());
        }
    });

    String assetsPic = "<img width='42px' height='58px' src='https://appassets.androidplatform.net/assets/pic.png'/>";
    String storagePic = "<img width='42px' height='58px' src='https://appassets.androidplatform.net/images/pic.jpg'/>";
    webView.loadData(assetsPic+"<br>"+storagePic, "text/html", "UTF-8");
票数 0
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42266264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档