简体   繁体   English

webview.loadData()不会加载本地图像

[英]webview.loadData() won't load local images

I have an html page that's built dynamically. 我有一个动态构建的html页面。 Some of the images come from an internet url and others i want to load from the Assets folder. 一些图像来自互联网网址和其他我想从Assets文件夹加载的图像。 The pages correctly renders the internet images, however, it just refuses to load local images. 页面正确呈现互联网图像,但是,它只是拒绝加载本地图像。 I'm tried many variations including the following and none will load the local images. 我尝试了很多变种,包括以下内容,没有一个会加载本地图像。 If everything is loaded from the internet url, the entire pages renders correctly but I really don't want to go to the internet for static content that won't change. 如果所有内容都是从互联网网址加载的,那么整个页面都会正确呈现,但我真的不想去互联网寻找不会改变的静态内容。 Any ideas? 有任何想法吗?

String pageData="a bunch of html markup..."; String pageData =“一堆html标记......”;

//-- i had to remove the < from the img tag below to get by the spam filter on this forum--> // - 我必须从下面的img标签中删除<通过此论坛上的垃圾邮件过滤器 - >

//--try 1--> pageData += "img src='file:///data/data/(my package)/files/image1.png' alt='my image'>"; // - 尝试1 - > pageData + =“img src ='file:/// data / data /(我的包)/files/image1.png'alt ='我的形象'>”;

//--try 2--> pageData += "img src='/data/data/(my package)/image1.png' alt='my image'>"; // - 尝试2 - > pageData + =“img src ='/ data / data /(我的包)/image1.png'alt ='我的形象'>”;

//--try 3--> pageData += "img src=' src='data:image/jpeg;base64,'" + readAssetFileAsBase64("image1.png") + "' alt='my image'>"; // - 尝试3 - > pageData + =“img src ='src ='data:image / jpeg; base64,'”+ readAssetFileAsBase64(“image1.png”)+“'alt ='my image'>” ;

//--try 4--> explosives! // - 尝试4 - >炸药!

The page is rendered using.. 页面使用..呈现

webview.loadData(pageData,"text/html",null); webview.loadData(pageData, “text / html的”,NULL);

but I've also tried 但我也试过了

webview.loadDataWithBaseURL("file://", s, "text/html","windows-1252" ,null); webview.loadDataWithBaseURL(“file://”,s,“text / html”,“windows-1252”,null);

I can't use .loadUrl() since the page is built dynamically. 我不能使用.loadUrl(),因为页面是动态构建的。 I suppose I could write the dynamic page out to disk the call .loadUrl() but that doesn't seem so efficient. 我想我可以将动态页面写入磁盘调用.loadUrl(),但这看起来效率不高。

You need to make sure you load the html with a base url, although that base url doesn't need to be anything pertinent 您需要确保使用基本URL加载html,尽管该基本URL不需要任何相关内容

webView.loadDataWithBaseURL("fake://not/needed", htmlString, mimeType, encoding, "");

and then you can reference things in the assets folder using something like this 然后你可以使用类似的东西引用assets文件夹中的东西

file:///android_asset/image1.jpg

the "android_asset" path seems to be the thing you haven't tried yet. “android_asset”路径似乎是你还没有尝试过的东西。 If your files are not in your asset folder then you need to serve them with a ContentProvider, see this tutorial from my site: http://responsiveandroid.com/2012/02/20/serving-android-webview-resources-with-content-providers.html 如果您的文件不在资产文件夹中,那么您需要使用ContentProvider提供它们,请参阅我的网站上的本教程: http//responsiveandroid.com/2012/02/20/serving-android-webview-resources-with-内容providers.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM