MainActivity.class
package cn.bgs.httpurlconnection;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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 java.net.URLConnection;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
private Button mBtn;
private ImageView mImg;
private String strPath="http://p2.so.qhimg.com/sdr/720_1080_/t01a4d0d142596d4be7.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mBtn=(Button) findViewById(R.id.mBtn);
mImg=(ImageView) findViewById(R.id.mImg);
mBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url=new URL(strPath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(50*1000);
connection.setConnectTimeout(50*1000);
connection.setDoInput(true);
connection.setDoOutput(true);
//设置请求方式
connection.setRequestMethod("POST");
InputStream is = connection.getInputStream();
String path=Environment.getExternalStorageDirectory()+"/dameinv.jpg";
File file=new File(path);
BufferedInputStream bis=new BufferedInputStream(is);
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(file));
byte [] by=new byte[1024];
int len=0;
while((len=bis.read(by))!=-1){
bos.write(by, 0, len);
}
bos.flush();
bos.close();
bis.close();
is.close();
Bitmap bit=BitmapFactory.decodeFile(path);
Message msg=handler.obtainMessage();
msg.what=1;
msg.obj=bit;
handler.sendMessage(msg);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==1) {
Bitmap bitmap=(Bitmap) msg.obj;
mImg.setImageBitmap(bitmap);
}
}
};
}
XML
activity_main.XML
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/mBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击下载图片"
/>
<ImageView
android:id="@+id/mImg"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>