Android Internet access method for obtaining images on the network

Source: Internet
Author: User
Tags sendmsg

Android Internet access method for obtaining images on the network

To obtain images on the network, you must first have the network access permission. This is not written anymore. It has been written in previous articles, and the layout method is no longer written, writing is also a simple layout. It doesn't make much sense to directly use the core code:

This is a simple class I use to obtain online images.

Public static Bitmap getImage (String Url) throws Exception {

Try {

URL url = new URL (Url );

String responseCode = url. openConnection (). getHeaderField (0 );

If (responseCode. indexOf ("200") <0)

Throw new Exception ("the image file does not exist or the path is incorrect. Error Code:" + responseCode );

Return BitmapFactory. decodeStream (url. openStream ());

} Catch (IOException e ){

// TODO Auto-generated catch block

Throw new Exception (e. getMessage ());

}

}

This class returns a Bitmap object,

The following is a simple call to the class:

Directly run the Code:

Bitmap mBitmap;

// Directly retrieve the image:

Private void RefreshDB (){

Try {

SendMSG (Declare. START, "loading image ......");

MBitmap = DownFile. getImage (image address );

SendMSG (Declare. STOP ,"");

} Catch (Exception e ){

// TODO Auto-generated catch block

SendMSG (Declare. ERROR, e. getMessage ());

}

A simple scroll bar is provided here, indicating that the current program is being executed and implemented by sending messages. The core code is not written anymore.

Run the following command after loading:

MImageView1.setImageBitmap (mBitmap );

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.