AndroidManifest.xml文件中加入如下權限設置:java
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />android
activity_image.xml代碼web
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.jt.http_01.LoadImage" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>
HttpLoadImage.java代碼網絡
/** * 加載網絡圖片(先下載到本地SD卡,再讀取本地文件顯示圖片) * @author jiatao * @date 2015-5-2 * @version 1.0 */ package com.jt.http_01; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.ImageView; public class HttpLoadImage extends Activity { private ImageView imageView; private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image); initDisplay(); } public void initDisplay() { // TODO Auto-generated method stub imageView = (ImageView) findViewById(R.id.imageView1); new HttpImageThread("http://img31.mtime.cn/pi/2015/02/03/111506.56197775_1000X1000.jpg", imageView, handler).start(); } }
HttpImageThread.java代碼app
/** * 加載網絡圖片(先下載到本地SD卡,再讀取本地文件顯示圖片) * @author jiatao * @date 2015-5-2 * @version 1.0 */ package com.jt.http_01; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Environment; import android.os.Handler; import android.widget.ImageView; public class HttpImageThread extends Thread { private String url; private ImageView imageView; private Handler handler; public HttpImageThread(String url,ImageView imageView,Handler handler){ this.url = url; this.imageView = imageView; this.handler = handler; } @Override public void run() { // TODO Auto-generated method stub super.run(); try { URL httpUrl = new URL(url); try { HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); conn.setReadTimeout(5000); conn.setRequestMethod("GET"); conn.setDoInput(true); //獲取字節輸入流 InputStream in = conn.getInputStream(); FileOutputStream out = null; File downloadfile = null; String fileName = String.valueOf(System.currentTimeMillis()); //判斷SD卡是否存在 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ File parent = Environment.getExternalStorageDirectory(); downloadfile = new File(parent,fileName); //在SD卡中建立圖片輸出目錄和圖片名稱 out = new FileOutputStream(downloadfile); } byte[] b = new byte[2*1024]; int len; if(out != null){ //若是輸出不爲空,就循環讀取下載到本地 while((len = in.read(b))!=-1){ out.write(b, 0, len); } } //BitmapFactory.decodeFile(String pathName)把本地文件轉化成Bitmap文件 final Bitmap bitmap = BitmapFactory.decodeFile(downloadfile.getAbsolutePath()); //向主線程發送消息,加載Bitmap文件 handler.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub imageView.setImageBitmap(bitmap); } }); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }