1)只显示网络图片
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) { //显示从网上下载的图片 imageview.setImageBitmap(bitmap); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageview=(ImageView)findViewById(R.id.imageView1); //创建并启动一个新线程用于从网络上下载图片 new Thread(){ @Override public void run() { try { //创建一个url对象 URL url=new URL("http://www.baidu.com/img/bdlogo.png"); //打开URL对应的资源输入流 InputStream is= url.openStream(); //从InputStream流中解析出图片 bitmap = BitmapFactory.decodeStream(is); // imageview.setImageBitmap(bitmap); //发送消息,通知UI组件显示图片 handler.sendEmptyMessage(0x9527); //关闭输入流 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)如果我们要显示并下载网络图片,只需要将MainActivity.java文件修改如下:
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) { //显示从网上下载的图片 imageview.setImageBitmap(bitmap); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageview=(ImageView)findViewById(R.id.imageView1); //创建并启动一个新线程用于从网络上下载图片 //这里必须新起一个线程,Android后面的版本中,不允许在主线程中联网 new Thread(){ @Override public void run() { try { //创建一个url对象 URL url=new URL("http://www.baidu.com/img/bdlogo.png"); //打开URL对应的资源输入流 InputStream is= url.openStream(); //从InputStream流中解析出图片 bitmap = BitmapFactory.decodeStream(is); // imageview.setImageBitmap(bitmap); //发送消息,通知UI组件显示图片 handler.sendEmptyMessage(0x9527); //关闭输入流 is.close(); //------------------------------ //下载网络图片 //再次打开资源流 is = url.openStream(); //打开手机对应的输出流 //存放在手机中,并命名为baidulogo.png OutputStream os = openFileOutput("baidulogo.png", MODE_WORLD_READABLE); byte[] buff = new byte[1024]; int len = 0; //因为网络下载一般不可能一次下载完毕,我们将每次下载好的有效数据写入 while ((len = is.read(buff)) > 0) { os.write(buff,0,len); } //关闭流 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); }}