Getting started with Android message mechanism and getting started with android Mechanism

Source: Internet
Author: User

Getting started with Android message mechanism and getting started with android Mechanism

Next, we will handle problems in Android Network Image Viewer.

Use the Add sub-thread to modify the original program:

Package com. wuyudong. imagesviewer; import java. io. inputStream; import java.net. httpURLConnection; import java.net. malformedURLException; import java.net. URL; import org. apache. http. httpConnection; import android. OS. bundle; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. text. textUtils; import android. view. menu; import android. view. view; import android. widget. editText; import android. widget. imageView; import android. widget. toast; public class MainActivity extends Activity {private EditText et_path; private ImageView iv; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); et_path = (EditText) findViewById (R. id. et_path); iv = (ImageView) findViewById (R. id. iv);} public void click (View view) {final String path = et_path.getText (). toString (). trim (); if (TextUtils. isEmpty (path) {Toast. makeText (this, "image path cannot be blank", 0 ). show () ;}else {new Thread () {@ Override public void run () {// connect to the Server get request to obtain the image try {URL url = new URL (path ); // send the http request HttpURLConnection conn = (HttpURLConnection) url according to the url. openConnection (); // set the Request Method conn. setRequestMethod ("GET"); conn. setConnectTimeout (5000); conn. setreadtimeouts (5000); conn. setRequestProperty ("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36 "); int code = conn. getResponseCode (); if (code = 200) {InputStream is = conn. getInputStream (); Bitmap bitmap = BitmapFactory. decodeStream (is); iv. setImageBitmap (bitmap);} else {Toast. makeText (MainActivity. this, "image display failed", 0 ). show () ;}} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace (); Toast. makeText (MainActivity. this, "failed to access image retrieval", 0 ). show ();}}}. start ();}}}

An error is reported after the project is run:

06-27 19:27:59. 613: W/System. err (2471): android. view. ViewRootImpl $ CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Only when a view object is created can the view be modified.

Code: iv. setImageBitmap (bitmap); the purpose is to modify the UI

That is, the main thread can be modified. Set to control multi-thread modification of view synchronization problems

The modified code is as follows:

Package com. wuyudong. imagesviewer; import java. io. inputStream; import java.net. httpURLConnection; import java.net. malformedURLException; import java.net. URL; import org. apache. http. httpConnection; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. text. textUtils; import android. view. menu; import android. view. view; import android. widget. editText; import android. widget. imageView; import android. widget. toast; public class MainActivity extends Activity {protected static final int CHANGE_UI = 1; protected static final int ERROR = 2; private EditText et_path; private ImageView iv; // 1. The main thread creates the message processor private Handler handler = new Handler () {public void handleMessage (android. OS. message msg) {if (msg. what = CHANGE_UI) {Bitmap bitmap = (Bitmap) msg. obj; iv. setImageBitmap (bitmap);} else if (msg. what = ERROR) {Toast. makeText (MainActivity. this, "failed to access image retrieval", 0 ). show () ;};};@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); et_path = (EditText) findViewById (R. id. et_path); iv = (ImageView) findViewById (R. id. iv);} public void click (View view) {final String path = et_path.getText (). toString (). trim (); if (TextUtils. isEmpty (path) {Toast. makeText (this, "image path cannot be blank", 0 ). show () ;}else {new Thread () {@ Override public void run () {// connect to the Server get request to obtain the image try {URL url = new URL (path ); // send the http request HttpURLConnection conn = (HttpURLConnection) url according to the url. openConnection (); // set the Request Method conn. setRequestMethod ("GET"); conn. setConnectTimeout (5000); conn. setreadtimeouts (5000); conn. setRequestProperty ("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36 "); // get the response code int code = conn. getResponseCode (); if (code = 200) {InputStream is = conn. getInputStream (); Bitmap bitmap = BitmapFactory. decodeStream (is); // iv. setImageBitmap (bitmap); // TODO: Tell the main thread a Message: help me change the interface, content: bitmap Message msg = new Message (); msg. what = CHANGE_UI; msg. obj = bitmap; handler. sendMessage (msg);} else {// Toast. makeText (MainActivity. this, "failed to show image", 0 )//. show (); Message msg = new Message (); msg. what = ERROR; handler. sendMessage (msg) ;}} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace (); // Toast. makeText (MainActivity. this, "failed to access image retrieval", // 0 ). show (); Message msg = new Message (); msg. what = ERROR; handler. sendMessage (msg );}}}. start ();}}}

 

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.