Android edittext loading HTML content (content includes web images)

Source: Internet
Author: User

Android html.fromhtml can be used to load the contents of the HTML, fromhtml have three parameters to set, the first is to display the HTML content, the second is to say the focus, imagegetter, used to handle the loading of the picture, a third Taghandler can be set to NULL, and then we explain the next imagegetter, many of the online methods are directly referring to local images, is synchronous, such as:

Private Imagegetter Imagegetter = new Imagegetter () {@Overridepublic drawable getdrawable (String source) {String F = pic_p Ath.substring (0, 1); String URL = pic_path.substring (2), if (F.equals ("1")) {try {contentresolver CR = Getactivity (). Getcontentresolver (); Uri uri = uri.parse (URL); Bitmap Bitmap = GetImage (CR, URI); return getmydrawable (Bitmap);} catch (Exception e) {e.printstacktrace (); return null;}} else {return null;}};
The above code is a project I made a reference to the local image method, rewrite Imagegetter, and then use Contentresolver to read the picture converted to bitmap, and then display, but, many times we will need to refer to the network image, Then this method won't work. Find a lot of information, if directly in the inside with the asynchronous method to load the picture, the display is a square point, then the question, how should we load the network Picture it?

first, let's start by creating a urldrawable, let it inherit . bitmapdrawable, rewrite the draw method, what's the use of this? This allows you to load the image with the initial image, which is the loaded image.

Urldrawable.java:

public class Urldrawable extends Bitmapdrawable {    //The drawable this need to set, you could set the initial draw ing    //with the loading image if you need to    protected drawable drawable;    @Override public    void Draw (canvas canvas) {        //Override The draw to facilitate refresh function later        if (DRA Wable! = null) {            drawable.draw (canvas);}}    }

The next step is to rewrite Imagegetter

Urlimageparser Inheritance Imagegetter

Release source

Urlimageparser.java

public class Urlimageparser implements Imagegetter {Context C;    EditText container; /*** * Build Urlimageparser will execute Asynctask, refresh container * @param t * @param c */public Urlimageparser (EditText T, Con        Text c) {this.c = C;    This.container = t;        Public drawable getdrawable (String source) {urldrawable urldrawable = new urldrawable ();        Get the actual source imagegetterasynctask Asynctask = new Imagegetterasynctask (urldrawable);        Asynctask.execute (source);    Return reference urldrawable I will change from SRC with actual image tag return urldrawable;        } public class Imagegetterasynctask extends Asynctask<string, Void, drawable> {urldrawable urldrawable;        Public Imagegetterasynctask (urldrawable d) {this.urldrawable = D;            } @Override protected drawable doinbackground (String ... params) {string source = Params[0];        return fetchdrawable (source); } @Override ProteCTED void OnPostExecute (drawable result) {//sets the correct binding based on the result of the HTTP call log.d ("height", "" "+result.getintrinsiche             Ight ());             LOG.D ("width", "" +result.getintrinsicwidth ());              Urldrawable.setbounds (0, 0, 0+result.getintrinsicwidth (), 0+result.getintrinsicheight ());             Change the current extractable reference result from http call urldrawable.drawable = result;            Draw Image Container URLImageParser.this.container.invalidate (); For ICS URLImageParser.this.container.setHeight ((URLImageParser.this.container.getHeight () + Result.getintri            Nsicheight ()));        Pre ICS URLImageParser.this.container.setEllipsize (null); }/*** * Get drawable URL * @param urlstring * @return * */Public drawable FET                Chdrawable (String urlstring) {try {InputStream is = fetch (urlstring);                drawable drawable = Drawable.createfromstream (IS, "src"); DrawablE.setbounds (0, 0, 0 + drawable.getintrinsicwidth (), 0 + drawable.getintrinsicheight ());            return drawable;            } catch (Exception e) {return null; }} private InputStream fetch (String urlstring) throws Malformedurlexception, IOException {Defa            Ulthttpclient httpClient = new Defaulthttpclient ();            HttpGet request = new HttpGet (urlstring);            HttpResponse response = Httpclient.execute (request);        Return response.getentity (). getcontent (); }    }}

The comments in the code are also very clear, there is no need to repeat the explanation, the most important thing is to rewrite OnPostExecute, this method is loaded after the UI refresh, you need to redraw the drawable to be displayed in EditText, And it doesn't overlap with the position of the text. Isn't that very simple?



Android edittext loading HTML content (content includes web images)

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.