我正在尝试使用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文件中。

我该如何解决?

最佳答案

您可以使用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" />

10-07 12:45
查看更多