【问题标题】:html5 apps for tablets: possible to load images from device's filesystem?平板电脑的 html5 应用程序:可以从设备的文件系统加载图像吗?
【发布时间】:2011-09-01 13:11:55
【问题描述】:

我创建了一个简单的 html 文件,它从我的本地硬盘驱动器 (ubuntu) 加载一些图像。 放就够了

 <img src=/home/user/directory/image.jpg></img>

现在我需要知道 Html5 在 Android 或 iOS 等平板电脑上显示时是否相同,或者 Html5 在离线应用中使用时是否相同。 我的意思是,如果 html5 可以像在我的计算机上一样从设备的文件系统中加载图像,而无需 localStorage 或 sessionStorage。

【问题讨论】:

    标签: android ios html storage


    【解决方案1】:

    如果您将应用程序部署为本机应用程序,这是可能的(用 Phonegap 包装)。

    对于保存的 HTML 文件,这是不可能的。

    【讨论】:

    • 我觉得PhoneGap都能做到,程序员也能做到,下面的诀窍是什么?
    • 你没有告诉你正在使用 WebView 进行开发 - 我假设你有 0% 的原生应用程序体验
    【解决方案2】:

    在 Android 上,虽然起初看起来有点棘手,但也可以完成。假设您在 layout.xml 中定义了一个 WebView,您想用应用程序附带的 html 文件填充该文件,该文件又应导入应用程序附带的 png。

    诀窍是将html文件放入res/raw,将png放入assets。

    示例。

    假设你有hello.html,其中应该包括buongiorno.png。

    1. 在您的项目中,例如MyProject,将buongiorno.png 放入MyProject/assets。

    2. hello.html 进入MyProject/res/raw(因为我们希望避免它被 android 资源编译器“优化”),并且可能如下所示:

      头>

      世界你好。

      正文>

    在您的 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多