Android Display Network picture instance _android

Source: Internet
Author: User

This example describes the Android display network image method, share for everyone to reference. The specific methods are as follows:

In general, displaying a Web image in Android is very simple, and here's a very simple example:

Step 1:

① Create your activity, in this case the viewwebimageactivity description;
The code in ②viewwebimageactivity is as follows:

Copy Code code as follows:
String imageUrl = "Yun_qi_img/logo.gif"; This is the web image you need to display---the Internet.
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;
}

③ among them, the Returnbitmap (String URL) method is to implement the network image conversion into bitmap.

Step 2:

Modify your Main.xml file as follows:

Copy Code code 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:

Add it to the node of your Androidmanifest.xml file, because Android has a lot of restrictions on the permissions, otherwise the images can't be displayed on your emulator.

I hope this article will help you with your Android program.

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.