How Android uses threads to get online images _android

Source: Internet
Author: User

This article for everyone to share the Android thread to get the network pictures of the specific code for your reference, the specific content as follows

Androidmanifest.xml

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
  package=" Com.zdcrobot.handlermessage ">
  <uses-permission android:name=" Android.permission.INTERNET "></uses-permission>
  <application
    android:allowbackup=" true "
    android:icon= "@mipmap/ic_launcher"
    android:label= "@string/app_name"
    android:supportsrtl= "true"
    android:theme= "@style/apptheme" >
    <activity
      android:name= ". Mainactivity "
      android:label=" @string/app_name "
      android:theme=" @style/apptheme.noactionbar ">
      <intent-filter>
        <action android:name= "Android.intent.action.MAIN"/>
 
        <category android: Name= "Android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>
  </ Application>
 
</manifest>

Activity_main.xml

 <?xml version= "1.0" encoding= "Utf-8"?> < Android.support.design.widget.CoordinatorLayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns : app= "Http://schemas.android.com/apk/res-auto" xmlns:tools= "Http://schemas.android.com/tools" Android:layout_ Width= "Match_parent" android:layout_height= "Match_parent" android:fitssystemwindows= "true" tools:context= " Com.zdcrobot.handlermessage.MainActivity "> <linearlayout android:orientation=" vertical "android:layout_w Idth= "Match_parent" android:layout_height= "match_parent" > <button android:id= "@+id/button1" Andr Oid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Load Picture"/> <ImageVie W android:id= "@+id/image1" android:layout_width= "match_parent" android:layout_height= "500DP"/> ;/linearlayout> </android.support.design.widget.coordinatorlayout> 

mainactivity.class   

Package com.zdcrobot.handlermessage;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import android.support.v7.app.AppCompatActivity;
Import Android.view.View;
Import Android.widget.Button;
 
Import Android.widget.ImageView;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
 
Import Java.net.URL;
  public class Mainactivity extends appcompatactivity {private button button;
  Private ImageView ImageView;
  Private String Imagpath = "Yun_qi_img/block.gif";
  Private final int is_finish = 1; Private Handler Handler = new Handler () {@Override public void Handlemessage (msg) {Bitmap Bitmap =
      (Bitmap) Msg.obj;
    Imageview.setimagebitmap (bitmap);
  }
  };
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R. Layout.activity_main);
    Button = (button) Findviewbyid (R.id.button1);
    ImageView = (ImageView) Findviewbyid (r.id.image1); Button.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {new Thr
      EAD (New MyClass ()). Start ();
  }
    });
      The public class MyClass implements runnable{@Override the public void Run () {Bitmap Bitmap = null;
        try {URL url = new URL (imagpath);
        HttpURLConnection httpurlconnection = (httpurlconnection) url.openconnection ();
        Httpurlconnection.setdoinput (TRUE);
        Httpurlconnection.connect ();
        InputStream InputStream = Httpurlconnection.getinputstream ();
      Bitmap = Bitmapfactory.decodestream (InputStream);
      catch (Malformedurlexception e) {e.printstacktrace ();
      catch (IOException e) {e.printstacktrace ();
      } message = Message.obtain ();
      Message.obj = bitmap; Message.what = Is_finish;
    Handler.sendmessage (message); }
  }
}

The above is the entire contents of this article, I hope to learn more about Android software programming help.

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.