我创建了一个简单的html文件,从我的本地硬盘驱动器(ubuntu)加载一些图像。
就足够了 <img src=/home/user/directory/image.jpg></img>
现在我需要知道在Android或iOS等平板电脑上显示Html5时是否相同,或者在离线应用中使用Html5。 我的意思是,如果html5可以加载来自设备文件系统的图像,就像在我的计算机上一样,没有localStorage或sessionStorage。
答案 0 :(得分:1)
如果您将应用程序部署为本机应用程序,则可以(使用Phonegap将其包装)。
对于保存的HTML文件,这是不可能的。
答案 1 :(得分:0)
在Android上,它可以完成,即使它起初看起来有点棘手。假设您已经在layout.xml中定义了一个WebView,您希望用您的应用程序附带的html文件填充该文件,而该文件又应该导入您的应用程序附带的png。
诀窍是将html文件放入res/raw
,将png放入assets
。
实施例
假设您有hello.html
,其中应包含buongiorno.png
。
在您的项目中,说MyProject
,将buongiorno.png
放入MyProject/assets
。
hello.html
进入MyProject/res/raw
(因为我们想避免让android资源编译器'优化'),看起来像这样:
<html> <head></head> <body> <img src="file:///android_asset/buongiorno.png"/> <p>Hello world.</p> </body> </html>
在你的java代码中,你可以输入以下代码:
WebView w = (WebView) findViewById(R.id.myWebview);
String html = getResourceAsString(context, R.raw.hello);
if (html != null) {
w.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
}
其中getResourceAsString()
的定义如下:
public static String getResourceAsString(Context context, int resid) throws NotFoundException {
Resources resources = context.getResources();
InputStream is = resources.openRawResource(resid);
try {
if (is != null && is.available() > 0) {
final byte[] data = new byte[is.available()];
is.read(data);
return new String(data);
}
} catch (IOException ioe) {
throw new RuntimeException(ioe);
} finally {
try {
is.close();
} catch (IOException ioe) {
// ignore
}
}
return null;
}