Android: imageview how to display network images

Source: Internet
Author: User

Android: imageview how to display network images
Address: http://hi.baidu.com/programmar/blog/item/79483ecb2ac75cf552664fd3.html

Displaying a network image in Android is actually super simple. The following is a very simple example:

Step 1:
1. Create Your activity, which is described in viewwebimageactivity in this example;
2. The code in viewwebimageactivity is as follows:
String imageurl = "http://hiphotos.baidu.com/baidu/pic/item/7d8aebfebf3f9e125c6008d8.jpg"; // This is the network picture you need to display --- online casually find
Bitmap bmimg;
Imageview imview;

Button button1;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Imview = (imageview) findviewbyid (R. Id. imview );
Imview. setimagebitmap (returnbitmap (imageurl ));
}

Public bitmap returnbitmap (string URL ){
URL myfileurl = NULL;
Bitmap bitmap = NULL;
Try {
Myfileurl = new URL (URL );
} Catch (malformedurlexception e ){
E. printstacktrace ();
}
Try {
Httpurlconnection conn = (httpurlconnection) myfileurl. openconnection ();
Conn. setdoinput (true );
Conn. Connect ();
Inputstream is = conn. getinputstream ();
Bitmap = bitmapfactory. decodestream (is );
Is. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
Return bitmap;
}

3. The returnbitmap (string URL) method is used to convert a network image to a bitmap.

Step 2:
1. Modify your main. xml file as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Imageview
Android: Id = "@ + ID/imview"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center"
/>
</Linearlayout>

Step 3:
1. In your androidmanifest. add <uses-Permission Android: Name = "android. permission. internet "/>. This is because android has a lot of permission restrictions. Otherwise, Images cannot be displayed on your simulator.

In this way, your goal has been achieved. It's easy, so don't try it now!

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.