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
| 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> |
/table>
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"/> |