"Android" webview read local pictures

Source: Internet
Author: User

Background

Our Blog Park app is a webview to show the details of the news. News must be illustrated, whether it is to support offline caching or speed up the opening of the news, we need to get through the local storage and WebView bridge between.

Idea 1: First set the absolute path of the webview to the root directory of our image store

Modify the first parameter to point to the local storage directory so that a relative path can be used to refer to local files in that directory. Such as

null);

2: Store the image resources locally

For the blog Park News entity, Dudu boss Intimate provided the ImageUrl field, the news to use the image address separated by semicolons, stitching into a string, reference example

This kind of data source is naturally easy and enjoyable for us to deal with.

But without this data source, we can also use regular expressions to traverse the news content of the IMG tag for processing.

Our local storage rules are like this, cut off the third/number before the content, will/convert to _ number stored in the cache folder.

Like Http://images2015.cnblogs.com/news/66372/201508/66372-20150826093938187-2008871077.jpg.

After processing is the news_66372_201508_66372-20150826093938187-2008871077.jpg file

3: Traverse the img tag, point src to local file

The regular expression of the label is as follows

Pattern patternimgsrc = Pattern.compile (" ");

In the loop of the regular traversal, we determine the current SRC path to match, whether the local presence, if present, replace the cost of the path, otherwise the use of the bitmap or direct download, depending on your wifi no map strategy.

4: Add JS interaction

In the News Details page, with the JS interaction has two places

1: If the user chooses to save traffic mode, when the picture is still occupied bitmap, the user click on the bitmap, you can load the original image replacement

2: If the image has been loaded, the user can click on the image to see a larger image

The knowledge here involves a small number of Android and JS Interaction, as well as JS processing picture tags.

The former online a lot of, don't elaborate, the latter code is very short, put it up

function ShowImage (obj,src) {    if (obj.src.indexOf (' click_load_day.png ') <0 && Obj.src.indexOf (' click_load_night.png ') <0)        android.displayimage (SRC);     Else         obj.src=src;}

The last of the following

Example: 1 for bitmap

2: Replace the bitmap with the original after clicking

3: Click on the replaced image, start the System native view Picture program activity.

Time a little rush, temporarily write here, details please refer to the station Blog Park app, source address

"Android" webview read local pictures

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.