Android programming shows examples of pictures on the network _android

Source: Internet
Author: User

The example in this article describes how Android programming displays pictures on the network. Share to everyone for your reference, specific as follows:

To display a picture on the network in Android, you need to find the image address based on the URL, then convert the image into a Java InputStream, and then convert the InputStream stream into a bitmap. Bitmap can be shown directly in the Android ImageView. This is the idea of displaying pictures on the Web, which is simple to achieve. Let's take a look at the implementation process below.

First, add access to the Internet to your program in Androidmanifest.xml:

Copy Code code as follows:
<uses-permissionandroid:name= "Android.permission.INTERNET"/>

Then add a imageview to the layout file to display the pictures on the network:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:layout_width=" fill_parent "
  android:layout_height=" fill_parent "
  android:o" rientation= "vertical" >
  <textview
    android:layout_width= "Fill_parent"
    Wrap_content "
    android:text=" @string/hello "/>
  <imageview android:layout_width=" Wrap_content "
    android:layout_height= "wrap_content"
    android:id= "@+id/imageview"/>
</LinearLayout>

In the activity of the main program, write the picture from the network and convert it into InputStream, then convert it into a bitmap that can be displayed in the ImageView.

Package com.image;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import android.app.Activity;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.os.Bundle;
Import Android.widget.ImageView; The public class Netimageactivity extends activity {/** called the ' when ' is the ' The activity ' is a-a-created. */String IMAGEURL =
   "Http://content.52pk.com/files/100623/2230_102437_1_lit.jpg";
   Bitmap bmimg;
  ImageView Imview;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Imview = (ImageView) Findviewbyid (R.id.imageview);
  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;

 }
}

Then run the program to display the picture on the network.

Operation Effect:

PS: About Androidmanifest.xml rights control details can refer to the site online tools:

Android manifest Features and Permissions description Encyclopedia:
Http://tools.jb51.net/table/AndroidManifest

For more information on Android-related content readers can view the site: "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course", "Android debugging techniques and common problems solution summary", " Android Multimedia How-to Summary (audio, video, audio, etc), summary of Android Basic components usage, Android View tips Summary, Android layout layout tips and a summary of Android controls usage

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

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.