package com.example.unti2_getpic_get_demo;
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.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView img;
private Handler myHandler=new Handler(){
public void handleMessage(android.os.Message msg) {
//显示
img.setImageBitmap((Bitmap)msg.obj);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.img);
}
public void getNetPic(View v){
// Caused by: android.os.NetworkOnMainThreadException
//sdd 3.0 在主线程中直接请求访问网络中的数据 3.0之后,访问网络时必须开启子线程
//
new Thread(){
public void run() {
requestNetPic();
};
}.start();
}
private void requestNetPic() {
String picPath="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506490888125&di=85752f75bcb1c8d612c2acb63021c1c7&imgtype=0&src=http%3A%2F%2Fpic25.photophoto.cn%2F20121216%2F0010023972522363_b.jpg";
try {
//1.根据路径创建出一个url对象
URL url=new URL(picPath);
//2.打开连接HttpURLConnection extends URLConnection 遵循http协议
HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
//3.设置
openConnection.setRequestMethod("GET");//get 是默认的请求方式
openConnection.setConnectTimeout(3000);//设置连接服务器的超时时间
openConnection.setReadTimeout(3000);//设置读取内容的超时时间
//4.得到返回的状态码
int responseCode = openConnection.getResponseCode();
//进行判断
if(responseCode==200){//请求成功
//5.拿到服务器返回的数据 以字节流的返回
InputStream inputStream = openConnection.getInputStream();
Log.d("zzz", inputStream.toString());
//将流转换成bitmap对象
Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
//发送数据
Message msg=Message.obtain();
msg.obj=bitmap;
myHandler.sendMessage(msg);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
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.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView img;
private Handler myHandler=new Handler(){
public void handleMessage(android.os.Message msg) {
//显示
img.setImageBitmap((Bitmap)msg.obj);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.img);
}
public void getNetPic(View v){
// Caused by: android.os.NetworkOnMainThreadException
//sdd 3.0 在主线程中直接请求访问网络中的数据 3.0之后,访问网络时必须开启子线程
//
new Thread(){
public void run() {
requestNetPic();
};
}.start();
}
private void requestNetPic() {
String picPath="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506490888125&di=85752f75bcb1c8d612c2acb63021c1c7&imgtype=0&src=http%3A%2F%2Fpic25.photophoto.cn%2F20121216%2F0010023972522363_b.jpg";
try {
//1.根据路径创建出一个url对象
URL url=new URL(picPath);
//2.打开连接HttpURLConnection extends URLConnection 遵循http协议
HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
//3.设置
openConnection.setRequestMethod("GET");//get 是默认的请求方式
openConnection.setConnectTimeout(3000);//设置连接服务器的超时时间
openConnection.setReadTimeout(3000);//设置读取内容的超时时间
//4.得到返回的状态码
int responseCode = openConnection.getResponseCode();
//进行判断
if(responseCode==200){//请求成功
//5.拿到服务器返回的数据 以字节流的返回
InputStream inputStream = openConnection.getInputStream();
Log.d("zzz", inputStream.toString());
//将流转换成bitmap对象
Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
//发送数据
Message msg=Message.obtain();
msg.obj=bitmap;
myHandler.sendMessage(msg);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}