xml文件代码部分
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
android:id="@+id/p_w_picpath"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
android:id="@+id/edit"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:text="http://e.hiphotos.baidu.com/p_w_picpath/pic/item/d52a2834349b033b6764dedb17ce36d3d439bdc6.jpg"
/>
android:id="@+id/go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go"
android:textSize="20sp"
/>
Activitypackage com.example.android01;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private ImageView p_w_picpath;
private EditText edit;
private Button but;
private final int success=0;
private Handler handler=new Handler(){
@Override
public void handleMessage(android.os.Message msg) {
Bitmap b=(Bitmap)msg.obj;
if(b!=null)
{
if(msg.what==success)
{
p_w_picpath.setImageBitmap(b);
}
}else{
Toast.makeText(getApplicationContext(), "获取图片错误", 0).show();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
p_w_picpath=(ImageView) findViewById(R.id.p_w_picpath);
edit=(EditText) findViewById(R.id.edit);
but=(Button) findViewById(R.id.go);
but.setOnClickListener(this);
}
@Override
public void onClick(View v) {
final String url=edit.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
Bitmap bit=getImageFromNet(url);
//p_w_picpath.setImageBitmap(bit);
Message msg=new Message();
msg.obj=bit;
msg.what=success;
handler.sendMessage(msg);
}
}).start();
}
private Bitmap getImageFromNet(String url){
HttpURLConnection conn=null;
try {
URL mURL=new URL(url);//创建一个URL连接
conn=(HttpURLConnection) mURL.openConnection();//得到一个connection对象
conn.setRequestMethod("GET");//设置请求方法为GET
conn.setConnectTimeout(10000);//设置连接超时时间
conn.setReadTimeout(5000);//设置读取过程中的异常
conn.connect();
int responseCode=conn.getResponseCode();//获取响应码,404,500,200
if(responseCode==200){
//访问成功
InputStream in=conn.getInputStream();
Bitmap bitmap=BitmapFactory.decodeStream(in);//将从服务器获取的流变成Bitmap位图
return bitmap;
}else{
Toast.makeText(this, "获取图片失败", 0).show();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(conn!=null)
{
conn.disconnect();
}
}
return null;
}
}