MainActivity主类
package com.example.http_02;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
/**
* author:潘海斌
* time:2015-08-08
*/
public class MainActivity extends Activity {
private ImageView imageView;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView1);
new HttpThread("http://www.shunmaixuexiao.com/yangweijie.jpg", imageView, handler).start();
}
}
HttpThread类
package com.example.http_02;
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 javax.net.ssl.HttpsURLConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import android.widget.ImageView;
public class HttpThread extends Thread {
private String url;
private ImageView imageView;
private Handler handler;
public HttpThread(String url,ImageView imageView,Handler handler){
this.url = url;
this.imageView = imageView;
this.handler = handler;
}
@Override
public void run() {
try {
//实例化URL对象,统一资源定位符对象
URL httpUrl = new URL(url);
try {
//HttpURLConnection也可以换成HttpsURLConnection,至于用哪个是根据URL的请求协议是http或https
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());
//MEDIA_MOUNTED代表SD卡正常挂载,此时目录存在,可以创建文件夹和文件
//只有在SD卡状态为MEDIA_MOUNTED时/mnt/sdcard目录才是可读可写,并且可以创建目录及文件
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//获得SD卡的目录
File parent = Environment.getExternalStorageDirectory();
downloadFile = new File(parent,fileName);//创建下载文件对象,第一个参数是文件路径,第二个是文件名
//创建一个文件输出流
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);//将文件写入流中
}
}
//创建bitmap,传进去参数为文件的路径
final Bitmap bitmap = BitmapFactory.decodeFile(downloadFile.getAbsolutePath());
handler.post(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
main.xml配置
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.http_01.MainActivity"
tools:ignore="MergeRootFrame" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
AndroidManifest.xml中权限配置
<!-- 访问网络的权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<!--在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!--往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>