Deferred loading is implemented in WebView, which is loaded only when the image is clicked.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.