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>