【问题标题】:Load images using html <img> tag from Android expansion file使用 Android 扩展文件中的 html <img> 标签加载图像
【发布时间】:2017-07-21 10:21:46
【问题描述】:

我正在尝试使用类似 WebView 从 Android 扩展文件 (.obb) 加载我的 HTML 文件

    String prompt = "";
    WebView webview = (WebView) rootView.findViewById(R.id.webview);

    try {
        InputStream html = getAssetFileDescriptor("www/" + mResult.getTitle() + ".html")
                .createInputStream();
        byte[] b = new byte[html.available()];
        html.read(b);
        prompt = new String(b);
        html.close();

    } catch (IOException e) {
        Log.e("Webview", "Couldn't open webpage");
    }

    webview.loadData(prompt, "text/html", "utf-8");

*getAssetFileDescriptor 返回 ZipResourceFile 函数(Zip 文件库)读取的 InputStream

它成功地将 HTML 加载到 Webview 中,但 HTML 中的所有图像都没有出现。

我猜是因为它无法通过img标签访问图像文件。

例如,我的 html 文件有类似的 img 标签

<img src="./image1.jpg" />

相对路径似乎不起作用,因为 html 和图像文件被压缩到 obb 文件中。

我该如何解决?

【问题讨论】:

    标签: android html path android-webview apk-expansion-files


    【解决方案1】:

    您可以使用 StorageManager 挂载 OBB 文件:

    StorageManager storageManager = (StorageManager)app.getSystemService(Context.STORAGE_SERVICE);
    boolean queued = storageManager.mountObb(<path>, null, new OnObbStateChangeListsner() {...});
    

    然后,您可以使用此路径来构造文件的路径:返回的路径是您的 OBB 的根:

    storageManager.getMountedObbPath(...) + "/image1.jpg";
    

    使用该路径加载图像

    <img src="your_path" />
    

    【讨论】:

    • 图片文件不在 assets 文件夹中。它们当前作为 .obb 文件存储在 /Android/obb//
    • 所以 src 应该是 .obb 文件的路径 + 文件中图像的路径,请检查编辑
    猜你喜欢
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 2015-01-08
    • 1970-01-01
    相关资源
    最近更新 更多