PublicClassImglabelactivityExtendsActivity {PrivateStaticFinal String TAG ="Imglabelactivity";/** Local Pictures */Private TextView Mtvone;/** Project Resource Picture */Private TextView Mtvtwo;/** Network Pictures * *Private TextView Mtvthree;/** Network Picture name*/Private String Picname ="Networkpic.jpg";/** Network Picture getter*/Private Networkimagegetter Mimagegetter;/** Network Picture Path */Private String Htmlthree ="Network Picture test:" +"@OverridePublicvoid OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate);Setcontentview (R.layout.activity_img_label);Mtvone = (TextView)This.findviewbyid (R.id.tv_img_label_one);String Htmlone ="Local picture test:" +"Mtvone.settext (html.fromhtml (Htmlone,New Localimagegetter (),NULL));Mtvtwo = (TextView)This.findviewbyid (R.id.tv_img_label_two);String Htmltwo ="Project Picture test:" +"<img src=""+r.drawable.imagepro+"">";Mtvtwo.settext (html.fromhtml (Htmltwo,New Proimagegetter (),NULL));Mtvthree = (TextView)This.findviewbyid (R.id.tv_img_label_three);Mimagegetter =New Networkimagegetter ();Mtvthree.settext (html.fromhtml (Htmlthree, Mimagegetter,null));}/** * Local image *@author Susie * *PrivateFinalClassLocalimagegetterImplementsHtml.imagegetter{ @Override Public drawable getdrawable (String source) { Get local pictures drawable drawable = Drawable.createfrompath (source); Must be set to the edge of the picture, otherwise textview not show the picture Drawable.setbounds (0,0, Drawable.getintrinsicwidth (), Drawable.getintrinsicheight ()); To return it return drawable;}}/** * Project Resource Picture *@author Susie * *PrivateFinalClassProimagegetterImplementsHtml.imagegetter{ @Override Public drawable getdrawable (String source) { Get to Resource ID int id = integer.parseint (source); drawable drawable = Getresources (). getdrawable (ID); Drawable.setbounds (0,0, Drawable.getintrinsicwidth (), Drawable.getintrinsicheight ()); return drawable;}}/** * Web Image *@author Susie * *PrivateFinalClassNetworkimagegetterImplementsHtml.imagegetter{ @Override Public drawable getdrawable (String source) { Drawable drawable =Null Package path File File =New File (Environment.getexternalstoragedirectory (), picname); Determine whether to start with HTTP if (Source.startswith ("http")) { Determine if a path exists if (file.exists ()) { There is the acquisition of drawable drawable = Drawable.createfrompath (File.getabsolutepath ()); Drawable.setbounds (0,0, Drawable.getintrinsicwidth (), Drawable.getintrinsicheight ()); }else { Do not exist open asynchronous task load network picture Asyncloadnetworkpic Networkpic =New Asyncloadnetworkpic (); Networkpic.execute (source); } } return drawable;}}/** * Load Network Picture Asynchronous class *@author Susie * *PrivateFinalClassAsyncloadnetworkpicExtendsasynctask<String,Integer,void>{ @Override Protected Void doinbackground (String ... params) { Load Network pictures Loadnetpic (params); ReturnNull} @Override Protectedvoid OnPostExecute (void result) { Super.onpostexecute (result); Once the execution is complete, set it once again Mtvthree.settext (html.fromhtml (Htmlthree, Mimagegetter,NULL));} /** Loading Network Pictures * * Privatevoid Loadnetpic (String ... params) { String Path = params[0]; File File =New File (Environment.getexternalstoragedirectory (), picname); InputStream in =Null FileOutputStream out =Null try { URL url =The new URL (path); HttpURLConnection Connurl = (httpurlconnection) url.openconnection (); Connurl.setconnecttimeout (5000); Connurl.setrequestmethod ("GET"); if (connurl.getresponsecode () = =200) { in = Connurl.getinputstream (); out =New FileOutputStream (file); byte[] Buffer =Newbyte[1024]; int Len; while (len = in.read (buffer))! =-1) { Out.write (Buffer,0, Len); } }else { LOG.I (TAG, Connurl.getresponsecode () +""); } }catch (Exception e) { E.printstacktrace (); }finally { if (In! =NULL) { try { In.close (); }catch (IOException e) { E.printstacktrace (); } } if (out! =null) { try { Out.close (); } catch (IOException e) { E.printsta Cktrace (); } } }}}
HTML class Imagegetter interface