Android: Load ImageView via URL

Source: Internet
Author: User

Two methods:

Method One:

Bitmap bimage= Getbitmapfromurl (Bannerpath); Image.setimagebitmap (bimage); Public StaticBitmap Getbitmapfromurl (String src) {Try{LOG.E ("src", SRC); URL url =NewURL (SRC);            HttpURLConnection connection = (httpurlconnection) url.openconnection (); Connection.setdoinput (true);            Connection.connect ();            InputStream input = Connection.getinputstream ();            Bitmap Mybitmap = Bitmapfactory.decodestream (input); LOG.E ("Bitmap", "returned");returnMybitmap; }Catch(IOException e)            {E.printstacktrace (); LOG.E ("Exception", E.getmessage ());return NULL; }    }

Or:

 Public StaticBitmap loadbitmap (String url) {Bitmap Bitmap =NULL; InputStream in =NULL; Bufferedoutputstream out =NULL;Try{in =NewBufferedinputstream (NewURL (URL). OpenStream (), io_buffer_size);FinalBytearrayoutputstream DataStream =NewBytearrayoutputstream (); out =NewBufferedoutputstream (DataStream, io_buffer_size);        Copy (in, out); Out.flush ();Final byte[] data = Datastream.tobytearray (); Bitmapfactory.options Options =NewBitmapfactory.options ();//options.insamplesize = 1;Bitmap = Bitmapfactory.decodebytearray (data, 0, data.length,options); }Catch(IOException e)    {LOG.E (TAG, "Could not load Bitmap from:" + URL); }finally{Closestream (in);    Closestream (out); }returnBitmap;}

Method 2

    drawable drawable = loadimagefromweboperations (Bannerpath);    Image.setimagedrawable (drawable);     Private drawable loadimagefromweboperations (String url)    {         try         {             new url (URL). GetContent ();             drawable D = Drawable.createfromstream (IS, "src name");             return D;         } Catch (Exception e) {             System.out.println ("exc=" +e);             return null;         }     }

Android: Load ImageView via URL

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.