Android Display Network image implementation code

Source: Internet
Author: User

It's actually a bit of a hassle when it comes to displaying a picture of the Web in Android. First you have to convert this network image into a Java Imputstream stream, and then convert this to a bitmap.
Bitmap can be passed as parameters to ImageView.

At the bottom of the Returnbitmap function is the core, but also can be reused, it is responsible for a URL of the network image into a local bitmap

The code is as follows Copy Code

Package com.jinyan.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.util.Log;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.ImageView;

public class Activitymain extends activity {
/** called the activity is a. */

String imageUrl = "Yun_qi_img/80199697.uas58yhk.50pxcross_of_the_knights_templar_svg.png";
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;
}

}

XML file

/table>
  code is as follows copy code

    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> 


Note that to get your program to access the network, you must add in the Menifest file:

The code is as follows Copy Code

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

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.