Android network image displayed on ImageView

Source: Internet
Author: User

In writing this blog post, I participated in a project development, which involves a lot of network calls related issues, I remember I just started to do the Android project, I encountered this problem before, then searched the internet, found a blog post, 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 it must be placed in the thread, and should not be placed directly in the OnCreate method, I have made some changes to the program, put them in the thread update will be able to.

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 join in the androidmanifest.xml? corresponding network permissions.


We created a new thread in the OnCreate method in Mainactivity.java, but we were also explicitly not able to manipulate the user interface in a non-UI thread, so we couldn't manipulate the UI directly in the new thread, so we would also post the change to the UI thread.

The above method executes without problems, thank you.


Android network image displayed on ImageView

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.