AndroidManifest.xml文件中加入以下权限设置:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE\_EXTERNAL\_STORAGE" />
activity_image.xml代码
1<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.jt.http_01.LoadImage" > 10 <ImageView 11 android:id="@+id/imageView1" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content"/> 14</RelativeLayout>
HttpLoadImage.java代码
1/** 2 * 加载网络图片(先下载到本地SD卡,再读取本地文件显示图片) 3 * @author jiatao 4 * @date 2015-5-2 5 * @version 1.0 6 */ 7package com.jt.http_01; 8import android.app.Activity; 9import android.os.Bundle; 10import android.os.Handler; 11import android.widget.ImageView; 12public class HttpLoadImage extends Activity { 13 14 private ImageView imageView; 15 private Handler handler = new Handler(); 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_image); 20 initDisplay(); 21 } 22 public void initDisplay() { 23 // TODO Auto-generated method stub 24 imageView = (ImageView) findViewById(R.id.imageView1); 25 new HttpImageThread("http://img31.mtime.cn/pi/2015/02/03/111506.56197775_1000X1000.jpg", imageView, handler).start(); 26 } 27}
HttpImageThread.java代码
1/** 2 * 加载网络图片(先下载到本地SD卡,再读取本地文件显示图片) 3 * @author jiatao 4 * @date 2015-5-2 5 * @version 1.0 6 */ 7package com.jt.http_01; 8import java.io.File; 9import java.io.FileOutputStream; 10import java.io.IOException; 11import java.io.InputStream; 12import java.net.HttpURLConnection; 13import java.net.MalformedURLException; 14import java.net.URL; 15import android.graphics.Bitmap; 16import android.graphics.BitmapFactory; 17import android.os.Environment; 18import android.os.Handler; 19import android.widget.ImageView; 20public class HttpImageThread extends Thread { 21 private String url; 22 private ImageView imageView; 23 private Handler handler; 24 25 public HttpImageThread(String url,ImageView imageView,Handler handler){ 26 this.url = url; 27 this.imageView = imageView; 28 this.handler = handler; 29 } 30 31 @Override 32 public void run() { 33 // TODO Auto-generated method stub 34 super.run(); 35 try { 36 URL httpUrl = new URL(url); 37 try { 38 HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); 39 conn.setReadTimeout(5000); 40 conn.setRequestMethod("GET"); 41 conn.setDoInput(true); 42 43 //获取字节输入流 44 InputStream in = conn.getInputStream(); 45 FileOutputStream out = null; 46 File downloadfile = null; 47 String fileName = String.valueOf(System.currentTimeMillis()); 48 //判断SD卡是否存在 49 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ 50 File parent = Environment.getExternalStorageDirectory(); 51 downloadfile = new File(parent,fileName); 52 //在SD卡中创建图片输出目录和图片名称 53 out = new FileOutputStream(downloadfile); 54 } 55 56 byte[] b = new byte[2*1024]; 57 int len; 58 if(out != null){ 59 //如果输出不为空,就循环读取下载到本地 60 while((len = in.read(b))!=-1){ 61 out.write(b, 0, len); 62 } 63 } 64 65 //BitmapFactory.decodeFile(String pathName)把本地文件转化成Bitmap文件 66 final Bitmap bitmap = BitmapFactory.decodeFile(downloadfile.getAbsolutePath()); 67 //向主线程发送消息,加载Bitmap文件 68 handler.post(new Runnable() { 69 70 @Override 71 public void run() { 72 // TODO Auto-generated method stub 73 imageView.setImageBitmap(bitmap); 74 } 75 }); 76 77 } catch (IOException e) { 78 // TODO Auto-generated catch block 79 e.printStackTrace(); 80 } 81 } catch (MalformedURLException e) { 82 // TODO Auto-generated catch block 83 e.printStackTrace(); 84 } 85 86 } 87 88}