Android uses ImageView to display network pictures

Source: Internet
Author: User

This case uses imageview simple implementation of the network image of the call. Attention to the issue. As a result of the use of the network, the use of the HttpClient method to access the network connection, about how, can participate in the article? Using HttpClient in Android to achieve HTTP communication effects? You need to be aware of configuring network permissions. and need to use new thread and handler to update activity, or will directly error not Main thread See example: Mainactivity.java
Package Com.example.imageview;import Android.os.bundle;import Android.os.handler;import android.app.Activity; Import Android.graphics.bitmap;import Android.view.menu;import Android.widget.imageview;public class MainActivity Extends Activity {private Bitmap BM = null, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); final Handler Handler = new Handler (); new Thread () {public void Run () {BM = new apachehttpclient (). Gethttpbmp ("http://www.qilujiaju.com/data/attachment/block/c9/ C960ba426890a8ddbfc35d2b4b0d97c9.jpg ") Handler.post (new Runnable () {@Overridepublic void Run () {//TODO Auto-generated method Stubimageview ImageView = (imageView) Findviewbyid (r.id.imageview1); Imageview.setimagebitmap ( BM);}});}. Start ();} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.activity_main, menu); return true;}}
Apachehttpclient.java
Package Com.example.imageview;import Java.io.ioexception;import Java.io.inputstream;import Org.apache.http.httpresponse;import Org.apache.http.httpstatus;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.methods.httpget;import Org.apache.http.impl.client.defaulthttpclient;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.util.log;public Class apachehttpclient {private static final String TAG = "Error";p ublic inputstream httpget (string url) {InputStream result = N Ull HttpClient HttpClient = new Defaulthttpclient (); HttpGet httpget = new HttpGet (URL); HttpResponse HttpResponse = null;try {HttpResponse = Httpclient.execute (httpget); int httpstatus = Httpresponse.getstatusline (). Getstatuscode (); if (httpstatus = = HTTPSTATUS.SC_OK) {InputStream in = Httpresponse.getentity (). getcontent (); try {result = in;} catch (Exception e) {log.i (TAG, "Exception");//TODO Auto-generated Catch BLOCKE.PRIntstacktrace ();}} else {result = null;}} catch (Clientprotocolexception e) {//TODO auto-generated catch Blocke.printstacktrace (); LOG.I (TAG, "clientprotocolexception");} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace (); LOG.I (TAG, "clientprotocolexception");} return result;} Public Bitmap gethttpbmp (String URL) {Bitmap BM = Null;inputstream are = httpget (URL); bm = Bitmapfactory.decodestream (is); r Eturn BM;}}
Androidmainfest.xml
<?

XML version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.imageview" android:versioncode= "1" android:versionname= "1.0" ><uses-sdkandroid: minsdkversion= "8" android:targetsdkversion= "/><uses-permission android:name=" Android.permission.INTERNET "/><applicationandroid:allowbackup=" true "android:icon=" @drawable/ic_launcher " Android:label= "@string/app_name" android:theme= "@style/apptheme" ><activityandroid:name= " Com.example.imageview.MainActivity "android:label=" @string/app_name "><intent-filter><action android: Name= "Android.intent.action.MAIN"/><category android:name= "Android.intent.category.LAUNCHER"/></ Intent-filter></activity></application></manifest>

Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context =". Mainactivity "><imageviewandroid:id=" @+id/imageview1 "android:layout_width=" Wrap_content "Android:layout_ height= "Wrap_content" android:layout_alignparentleft= "true" android:layout_alignparenttop= "true" android:src= "@ Drawable/app "/></relativelayout>
This is a complete example that can be executed directly on the emulator or the real machine.

Android uses ImageView to display network pictures

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.