In writing this blog post, I participated in a project development, which involves a lot of network calls related issues, I remember when I just started to do Android project, I have encountered this problem, then searched the internet, found a blog post, and now share with you, HTTP/ Www.open-open.com/lib/view/open1376128628881.html
In fact, the idea of this article is problematic, because the network is the need for constant polling access, so must be placed in the thread, and should not be placed directly in the OnCreate method, I have a certain modification of the program, put them in the thread update on the line can be.
The Mainactivity.java code for this thing:
Package Com.test.picture;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;public class Mainactivity extends Activity {private ImageView imageview;private String PicturePath = "http://content.52pk.com/files/100623/2230_102437_1_lit.jpg"; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Initview (); New Thread (New Runnable () {@Overridepublic void Run () {//TODO auto-generated method stubfinal Bitmap Bitmap = Returnbitmap (PicturePath) imageview.post (new Runnable () {@Overridepublic void Run () {//TODO Au To-generated method Stubimageview.setimagebitmap (bitmap);}}). Start ();} Private 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 (); I Nputstream is = Conn.getinputstream (), bitmap = Bitmapfactory.decodestream (is); Is.close (); catch (IOException e) {e.printstacktrace ();} return bitmap;} private void Initview () {ImageView = (ImageView) Findviewbyid (r.id.picture);}}
Here is the code for the layout file:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/display_image" android:layout_width= "match_parent" android:layout_height= "match_parent" android:gravity= "center" android:orientation= "vertical" > <imageview android:id= "@+id/picture" android:layout_ Width= "Wrap_content" android:layout_height= "Wrap_content"/></linearlayout>
Of course we also want to add the appropriate network permissions in the Androidmanifest.xml.
We created a new thread in the OnCreate method in Mainactivity.java, but we also understood that we could not manipulate the user interface in a non-UI thread, so we could not manipulate the UI directly in the new thread, so we would also post the change to the UI thread.
The above method runs without problems, thank you.
Android network image displayed on ImageView