Android uses a URL to display network images

Source: Internet
Author: User

Android uses a URL to display network images

 

 

The first several methods in a URL object are easy to understand, and the openStream () provided by this object can read the InputStream of the URL resource. This method can be used to conveniently read remote resources.

The following program demonstrates how to read remote resources through the URL class:

1) only show network images

2) display and download network images

 

1) only show network images

A) activity_main.xml

 

     
  
 

 

B) MainActivity. java

 

Package dujun. king. urlgetimage; import java. io. inputStream; import java.net. URL; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. menu; import android. view. menuItem; import android. widget. imageView; public class MainActivity extends Activity {Bitmap bitmap; ImageView imageview; Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {if (msg. what = 0x9527) {// display the image imageview downloaded from the Internet. setImageBitmap (bitmap) ;}}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); imageview = (ImageView) findViewById (R. id. imageView1); // create and start a new Thread for downloading images from the network new Thread () {@ Override public void run () {try {// create a url object URL url = new URL (http://www.baidu.com/img/bdlogo.png); // open the resource input stream corresponding to the URL InputStream is = url. openStream (); // parse the image bitmap = BitmapFactory from the InputStream. decodeStream (is); // imageview. setImageBitmap (bitmap); // sends a message to notify the UI component to display the image handler. sendEmptyMessage (0x9527); // close the input stream is. close ();} catch (Exception e) {e. printStackTrace ();}}}. start () ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}
C) AndroidMainifest. xml

 

 

 
     
  
   
                           
                                    
                 
     
                
   
  
 

2) to display and download the network image, you only need to modify the MainActivity. java file as follows:

 

Package dujun. king. urlgetimage; import java. io. inputStream; import java. io. outputStream; import java.net. URL; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. menu; import android. view. menuItem; import android. widget. imageView; public class MainActivity extends Activity {Bitmap bitmap; ImageView imageview; Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {if (msg. what = 0x9527) {// display the image imageview downloaded from the Internet. setImageBitmap (bitmap) ;}}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); imageview = (ImageView) findViewById (R. id. imageView1); // create and start a new thread for downloading images from the network. // a new thread is required here, which is later than Android, new Thread () {@ Override public void run () {try {// create a url object URL url = new URL (http://www.baidu.com/img/bdlogo.png ); // open the resource input stream (InputStream is = URL) corresponding to the url. openStream (); // parse the image bitmap = BitmapFactory from the InputStream. decodeStream (is); // imageview. setImageBitmap (bitmap); // sends a message to notify the UI component to display the image handler. sendEmptyMessage (0x9527); // close the input stream is. close (); // ------------------------------ // download the network image // open the resource stream again is = url. openStream (); // open the output stream corresponding to the mobile phone // export OutputStream OS = openFileOutput(baidulogo.png, MODE_WORLD_READABLE); byte [] buff = new byte [1024]; int len = 0; // because it is generally impossible to download a download over the internet, we write the valid data downloaded each time into the while (len = is. read (buff)> 0) {OS. write (buff, 0, len);} // close the stream is. close (); OS. close ();} catch (Exception e) {e. printStackTrace ();}}}. start () ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}

 


References: edited by Li Gang, crazy Android handout (version 2nd)

Address: http://blog.csdn.net/qingdujun/article/details/39271479

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.