Android webview loadDataWithBaseURL如何从资产加载图像?
在我的项目中,我有一个文件:Android webview loadDataWithBaseURL如何从资产加载图像?,android,url,webview,load,Android,Url,Webview,Load,在我的项目中,我有一个文件: "MyProject/assets/folder1/image1.jpg" "MyProject/assets/folder1/index.html". 在webView中,我需要打开index.html(带图像) 我正在尝试以下代码: String baseUrl = "file:///android_asset/folder1/"; webView.loadDataWithBaseURL(baseUrl, readFileAsString("index.htm
"MyProject/assets/folder1/image1.jpg"
"MyProject/assets/folder1/index.html".
在webView中,我需要打开index.html(带图像)
我正在尝试以下代码:
String baseUrl = "file:///android_asset/folder1/";
webView.loadDataWithBaseURL(baseUrl, readFileAsString("index.html") , mimeType, "UTF-8", null);
但是图像不能加载
如果我将图像放在“资产”目录(MyProject/assets/
)中,并生成baseUrl=”file:///android_asset“
正确加载图像
如何不仅从根资源目录加载图像,而且从
资源/folder1
加载图像?您是否已授予internet权限
<uses-permission android:name="android.permission.INTERNET" />
像这样试试
WebView webview = (WebView)this.findViewById(R.id.webview);
String html = "<html><head><title>TITLE!!!</title></head>";
html += "<body><h1>Image?</h1><img src=\"icon.png\" /></body></html>";
webview.loadDataWithBaseURL("file:///android_res/drawable/", html, "text/html", "UTF-8", null);
WebView-WebView=(WebView)this.findviewbyd(R.id.WebView);
字符串html=“TITLE!!!”;
html+=“图像?”;
webview.loadDataWithBaseURL(“file:///android_res/drawable/,html,“text/html”,“UTF-8”,null);
有关更多信息,请尝试此
完美试着喜欢这个
try {
String filePath = null;
filePath = "Your File path";
Bitmap bitmap = null;
bitmap = BitmapFactory.decodeFile(filePath);
Log.v("Image data-->", "" + bitmap);
imageWidth = bitmap.getWidth();
imageHeight = bitmap.getHeight();
Log.e("Width", "" + imageWidth);
filePath = "file://" + filePath;
String html = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html\";charset=utf-8\"/><title></title></head><body style=\"width:"
+ imageWidth
+ "px; height:"
+ imageHeight
+ "px; background:url("
+ filePath
+ ") no-repeat; position:relative;\">"
+ getDivTag(mapList)
+ "</body></html>";
Log.v("MIS", "" + html);
webview.getSettings().setSupportZoom(true);
webview.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
System.out.println(html);
} catch (Exception e) {
e.printStackTrace();
}
试试看{
字符串filePath=null;
filePath=“您的文件路径”;
位图=空;
位图=BitmapFactory.decodeFile(文件路径);
Log.v(“图像数据-->”,“”+位图);
imageWidth=bitmap.getWidth();
imageHeight=bitmap.getHeight();
Log.e(“宽度”、“图像宽度”);
filePath=“file://”+filePath;
String html=“我认为您必须将基础设置为资产,并将子文件夹添加到图像src中,如下所示:
webView.loadDataWithBaseURL(“file:///android_asset/,readAssetFileAsString(“folder1/index.html”),“text/html”,“UTF-8”,null);
Html:
这对我在安卓5.1上很有效
private String readAssetFileAsString(String sourceHtmlLocation)
{
InputStream is;
try
{
is = getContext().getAssets().open(sourceHtmlLocation);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
return new String(buffer, "UTF-8");
}
catch(IOException e)
{
e.printStackTrace();
}
return "";
}
说真的吗?如果他说把它们放在一个文件夹中效果很好,那就意味着他必须有权限。听说这个函数的baseURL
会忽略在first/之后的任何内容。baseURL允许你从文件夹中运行所有文件,例如.js/.css/.html文件。例如,有一个文件夹和所有.js/.css/.html文件在其中,您可以调用.html文件,在其中找到名为baseURL的文件夹地址:webview.loadDataWithBaseURL(“file:///android_asset/myWebFilesInThisFolder/“,”你好,世界“因此,你不必为JS/CSS/HTML编写完整地址,只需将其捆绑起来!添加了方法代码,该代码以前是从另一篇So文章中获取的,但不幸的是,我没有相关链接,我不久前看到了一篇关于此主题的长文章。如果你还需要帮助,请查阅。