The picture that is usually obtained from the server side is the URL address, if simply get the picture through the URL address?
There is an open source project: Smartimageview, do this function, while there are other features, download inconvenience, too large
This simple function of implementing it yourself here
Code:
PackageOrg.dreamtech.smartimageview;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL;ImportAndroid.annotation.SuppressLint;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.util.AttributeSet;ImportAndroid.widget.ImageView; Public classMysmartimageviewextendsImageView {protected Static Final intrequestsuccess = 1; protected Static Final intRequestfail = 2; protected Static Final intERROR = 3; @SuppressLint ("Handlerleak") PrivateHandler Handler =NewHandler () { Public voidhandlemessage (android.os.Message msg) {Switch(msg.what) { CaseRequestsuccess:bitmap Bitmap=(Bitmap) msg.obj; Mysmartimageview. This. Setimagebitmap (bitmap); Break; CaseRequestfail:intDefault_resource =(Integer) msg.obj; Mysmartimageview. This. Setbackgroundresource (Default_resource); Break; CaseERROR:intResource =(Integer) msg.obj; Mysmartimageview. This. Setbackgroundresource (Resource); Break; } }; }; //The construction methods of the parent class PublicMysmartimageview (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); } PublicMysmartimageview (Context context, AttributeSet attrs) {Super(context, attrs); } PublicMysmartimageview (Context context) {Super(context); } //A method of displaying pictures//path:the parameters of URL transmission Public voidSetimageurl (FinalString Path) { NewThread () { Public voidrun () {Try{URL URL=NewURL (path); HttpURLConnection Conn=(httpurlconnection) URL. OpenConnection (); Conn.setrequestmethod ("GET"); Conn.setconnecttimeout (5000); intCode =Conn.getresponsecode (); if(Code = = 200) {InputStream in=Conn.getinputstream (); Bitmap Bitmap=Bitmapfactory.decodestream (in); Message msg=Message.obtain (); Msg.obj=bitmap; Handler.sendmessage (msg); } } Catch(Exception e) {e.printstacktrace (); } }; }.start (); } //A method of displaying pictures (overloading method)//path:the parameters of URL transmission//Resource:default Resources (If You can ' t find a resource through this URL) Public voidSetimageurl (FinalString Path,Final intResource) { NewThread () { Public voidrun () {Try{URL URL=NewURL (path); HttpURLConnection Conn=(httpurlconnection) URL. OpenConnection (); Conn.setrequestmethod ("GET"); Conn.setconnecttimeout (5000); intCode =Conn.getresponsecode (); if(Code = = 200) {InputStream in=Conn.getinputstream (); Bitmap Bitmap=Bitmapfactory.decodestream (in); Message msg=Message.obtain (); Msg.what=requestsuccess; Msg.obj=bitmap; Handler.sendmessage (msg); } Else{Message msg=Message.obtain (); Msg.what=Requestfail; Msg.obj=resource; Handler.sendmessage (msg); } } Catch(Exception e) {Message msg=Message.obtain (); Msg.what=ERROR; Msg.obj=resource; Handler.sendmessage (msg); } }; }.start (); }}
Two overloaded methods:
1: Clear URL address is correct, not error, call directly
2: Prevent image URL error, set default resource, pass two parameters
Under test:
PackageOrg.dreamtech.smartimageview;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mysmartimageview IV=(Mysmartimageview) Findviewbyid (R.ID.IV); Iv.setimageurl ("Http://fanyi.bdstatic.com/static/translation/img/header/logo_cbfea26.png", R.drawable.default_ic); }}
Layout:
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity " > <Org.dreamtech.smartimageview.MySmartImageViewAndroid:id= "@+id/iv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></Relativelayout>
Here is a correct URL address, the result is as follows:
Next, I changed the URL address to the wrong:
Results:
All right, done!
Android Development: Smartimageview Simple Implementation and application