使用 html <img> 标签从 Android 扩展文件加载图像
Load images using html <img> tag from Android expansion file
我正在尝试使用
之类的 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 returns ZipResourceFile函数读取的InputStream(Zip文件库)
它成功地将 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" />
我正在尝试使用
之类的 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 returns ZipResourceFile函数读取的InputStream(Zip文件库)
它成功地将 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" />