Android obtains images through http

Source: Internet
Author: User

Android obtains images from the image URL and displays them in the imageView.

The following describes the procedure:

First, you must declare the imageView control in the layout file:

<ImageView
Android: id = "@ + id/image"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>

You must also add the network access permission to the list file:

<Uses-permission android: name = "android. permission. INTERNET"/>

Secondly, use a service class to access the http protocol and obtain the returned value of the link. The htmlPath is the network address of the image.

Public class PageService {
/** @ Description: obtains image data.
* @ Author: Administrator
* @ Return: byte []
* @ Param htmlpath
* @ Return
* @ Throws Exception
*/

Public static byte [] getImage (String htmlpath) throws Exception {
Byte [] imagearray = null;
URL url = new URL (htmlpath );
HttpURLConnection conn = (HttpURLConnection) url. openConnection ();
Conn. setConnectTimeout (5000 );
// Conn. setRequestMethod ("GET ");
Conn. connect ();
If (conn. getResponseCode () = 200 ){
Byte [] buffer = new byte [1024];
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream ();
Int len = 0;
InputStream inputStream = conn. getInputStream ();
While (len = inputStream. read (buffer ))! =-1 ){
ArrayOutputStream. write (buffer, 0, buffer. length );
}
Imagearray = arrayOutputStream. toByteArray ();
}
Return imagearray;

}
}
Finally, enable a thread in the activity to call this business method and update the UI in handler: (the thread must be implemented; otherwise, an error will occur, which is different from that before version 3.0)

Public class MainActivity extends Activity {
Private EditText strpath;
Private TextView htmlcontent;
Private ImageView imageview;
Handler handler = new Handler (){

@ Override
Public void handleMessage (Message msg ){
Byte [] data = (byte []) msg. obj;
Bitmap image = BitmapFactory. decodeByteArray (data, 0, data. length );
Imageview. setImageBitmap (image );}


The content in the thread is as follows:

Private final class ImageThread extends Thread {

@ Override
Public void run (){
String htmlpath = strpath. getText (). toString ();
Htmlpath = "http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif ";
Byte [] htmlarray = null;
Try {
Htmlarray = PageService. getImage (htmlpath );
} Catch (Exception e ){
E. printStackTrace ();
}
Message msg = new Message ();
Msg. obj = htmlarray;
Msg. what = 1;
Handler. sendMessage (msg );
}

}

Finally, click a button to trigger the image retrieval event:

Private final class ButtonClickListener implements View. OnClickListener {

@ Override
Public void onClick (View v ){
ImageThread thread = new ImageThread ();
Thread. start ();
}
}

Now, the introduction is complete.

If the obtained content is the html page content, if the content is too long, you need to use a scroll bar to display it:

You can add ScollView to the TextView to load the scroll bar:

<ScrollView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">

<TextView
Android: id = "@ + id/htmlcontent"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</ScrollView>

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.