1String newhtml = html +2"<script type=\" text/javascript\ ">" +3"(function () {" +4"var imageList = document.getElementsByTagName (\" img\ ");" +5"For (var i=0; i<imagelist.length; i++) {"+6"var image = Imagelist[i];" +7"Image.href = IMAGE.SRC;" +8"IMAGE.SRC = \" Content://com.example.demo/res/def.jpg\ ";" +9"Image.alt = \" Click Load Picture \ ";" +Ten"Image.onclick = function () {" + One"THIS.SRC = this.href;" + A"Return false;" + -"}"+ -"}"+ the"}());" + -"</script>";
-Mwebview.getsettings (). Setjavascriptenabled (True);
-Mwebview.loaddata (newhtml, "text/html; Charset=utf-8 ",NULL);
The above code is to add an automatic JS function after the obtained HTML page, replace the address of all the image labels with a placeholder bitmap, and then load the real picture when the picture is clicked.
To load the image that comes with the app, we can use ContentProvider to implement it. Create a ContentProvider in the Com.example.demo.provider package directory, override the Openassetfile method to get the bitmap in the Assets folder, and register the Androidmanifest.xml in Contentprov Ider
1 @Override2 Publicassetfiledescriptor openassetfile (URI Uri, String mode)3 throwsFileNotFoundException {4String FileName =uri.getlastpathsegment ();5 Try {6 returnGetContext (). Getassets (). OPENFD (fileName);7}Catch(IOException e) {8 e.printstacktrace ();9 return Super. Openassetfile (URI, mode);Ten } One}
1 < 2 android:name= "Com.example.demo.database.ImageContentProvider"3 android:authorities= "Com.example.demo"/>
However, this method can only be used in the following version of Android4.4, after the 4.4 version of the WebView kernel has been replaced by this approach is invalid. So we can use one of the webviewclient in the API11 to start providing an interface shouldinterceptrequest to achieve interception.
1 Publicwebresourceresponse shouldinterceptrequest (WebView view,2 String URL) {3Webresourceresponse response =NULL;4 if(Url.contains ("Def.jpg")) {5 Try {6InputStream image = Getassets (). Open ("Def.jpg");7Response =NewWebresourceresponse ("Image/jpeg", "UTF-8", image);8}Catch(IOException e) {9 e.printstacktrace ();Ten } One } A returnresponse; -}
The most convenient thing is to find a cloud storage, upload a picture to it. Then use the URL of this image as the bitmap, anyway WebView will automatically cache the data, so this image will only be read once.
Deferred loading is implemented in WebView, which is loaded only when the image is clicked.