AndroidManifest.xml文件中加入以下权限设置:
activity_image.xml代码
<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代码
/**
* 加载网络图片(先下载到本地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();
}
}
}