加载Web的应用程序到网页视图使用本地路径为存储在应用程序图像 [英] Load web-application into a webview using local paths for images stored in the application

查看:136
本文介绍了加载Web的应用程序到网页视图使用本地路径为存储在应用程序图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够创建一个使用的WebView请求URL的应用
从外部的Web应用程序返回的HTML和CSS的
引用是实际应用中的资产图像。该
想法基本上是加快的一切,让图像永远不会有
下载。

I want to be able to create an app that uses WebView to request a url from an external web application which returns html and css that references images that are assets within the actual application. The idea is basically to speed up everything so that images never have to be downloaded.

下面是一个简单的例子:

Here is a simplified example:

服务器HTML:

<html> 
  <head> 
  <style> 
     #myImage { background-image: url("file:///android_asset/myImage.jpg"; width: 50px; height: 50px;} 
  </style> 
  </head> 
  <body> 
    <div id="myImage"></div> 
  </body> 
</html>

那么,有没有什么办法能够做到这一点?我的主要目标是只让应用程序请求中的所有HTML从服务器,但能够在应用程序中的图像的URL对本地资源的映射。

So, is there any way to be able to do this? My main goal is to just have the application request all the HTML from a server, but be able to map the image urls to local resources within the application.

在此先感谢,
莱昂

Thanks in advance, Leon

推荐答案

为什么不加载从应用端的所有HTML?如果你懒得这个网页将不得不无法访问网络 - 使用WebView.loadDataWithBaseUrl方法

Why not to load all HTML from application side? If you bother that this web page will have no access to network - use WebView.loadDataWithBaseUrl method.

有关图像嵌入到网页中,你可以使用数据:URI方案: HTTP://en.wikipedia .ORG /维基/ Data_URI_scheme

For embed images into a web page you can use data:URI scheme: http://en.wikipedia.org/wiki/Data_URI_scheme

您也可以,即使你是网页远程加载地图应用程序中的图像。您可以使用WebView.loadUrl(JavaScript的:......),以通过JavaScript code送上的图像数据(也使用数据:URI方案)。

Also you can map your application images even if you are page is loaded remotely. You can use WebView.loadUrl("javascript:....") to "send" images data via JavaScript code (also using data:URI scheme).

修改

首先,在HTML端带有嵌入式图像的例子将是这个样子:

Firstly, at HTML side your example with embedded images will look something like this:

<html> 
  <head> 
  <style> 
     #myImage { background-image: url('data:image/png;base64,iVBORw0KG.....'); width: 50px; height: 50px;} 
  </style> 
  </head> 
  <body> 
    <div id="myImage"></div> 
  </body> 
</html>

在,如果你想这个页面在应用端存储,可以存储在某个地方(字符串资源,资产文件夹),如果得到它。

When, if you want to store this page at the application side, you can store it somewhere (string resource, asset folder) and when get it.

String pageResource = // get it somehow
WebView myWebView;

myWebView.loadDataWithBaseUrl(
    "http://my.site.com",  // The base url
    pageResource,          // page content to load...
    "text/html",           // it's MIME type...
    "UTF-8",               // and encoding
    "http://my.site.com/page.html");

现在的WebView加载页面。它是从本地资源加载但从web视图点它就像它是从网络加载。它拥有访问网络资源和JavaScript code在这里也工作(这是 loadData loadDataWithBaseUrl )。

这篇关于加载Web的应用程序到网页视图使用本地路径为存储在应用程序图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆