Android: Load imageview through URL

Source: Internet
Author: User

IPhoneThe above implementation is very simple, one lineCode:

Imageview.Image=[UiimageImagewithcontentsofurl:Theurl];

Android:

Two methods:

Bitmap bimage = Getbitmapfromurl (bannerpath); image. setimagebitmap (bimage );  Public   Static Bitmap getbitmapfromurl (string SRC ){  Try  {Log. E ( "Src" , Src); Url URL = New  URL (SRC); httpurlconnection connection = (Httpurlconnection) URL. openconnection (); connection. setdoinput (  True  ); Connection. Connect (); inputstream Input = Connection. getinputstream (); bitmap mybitmap =Bitmapfactory. decodestream (input); log. E ( "Bitmap", "returned" );  Return  Mybitmap ;}  Catch  (Ioexception e) {e. printstacktrace (); log. E ( "Exception" , E. getmessage ());  Return   Null  ;}} 

Or try it

 

 Public  Static  Bitmap loadbitmap (string URL) {Bitmap bitmap = Null  ; Inputstream in = Null  ; Bufferedoutputstream out = Null  ;  Try  {In = New Bufferedinputstream ( New  URL (URL). openstream (), io_buffer_size ); Final Bytearrayoutputstream datastream = New  Bytearrayoutputstream (); Out = New  Bufferedoutputstream (datastream, io_buffer_size); copy (In, out); Out. Flush ();  Final   Byte [] DATA = Datastream. tobytearray (); bitmapfactory. Options options = New  Bitmapfactory. Options ();  // Options. insamplesize = 1;  Bitmap = Bitmapfactory. decodebytearray (data, 0 , Data. length, options );}  Catch  (Ioexception e) {log. E (tag, "Cocould not load bitmap from:" + URL );}  Finally  {Closestream (in); closestream (out );}  Return  Bitmap ;} 

 

 

 

Method 2

 

Drawable = Loadimagefromweboperations (bannerpath); image. setimagedrawable (drawable );  Private  Drawable loadimagefromweboperations (string URL ){  Try  {Inputstream is = (Inputstream) New  URL (URL). getcontent (); drawable d = Drawable. createfromstream (is, "src name" );  Return D ;}  Catch  (Exception e) {system. Out. println ( "Exc =" + E );  Return   Null  ;}} 

 

Done!

 

Related Article

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.