网络图片浏览器——HttpURLConnection

来源:互联网 发布:js 添加dom 动画 编辑:程序博客网 时间:2024/06/05 09:38

这是我们今天要做的案例的运行效果图:

在这个案例中运用到了线程,Handler,HttpURLConnection几个知识点。

1.布局界面

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="cn.edu.bzu.httppicture.MainActivity">    <ImageView        android:id="@+id/iv_image"        android:layout_weight="1000"        android:layout_width="match_parent"        android:layout_height="match_parent" />    <EditText        android:id="@+id/et_path"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:lines="1"        android:text="http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="click"        android:text="浏览"        /></LinearLayout>
2.MainActivity

public class MainActivity extends AppCompatActivity {    private EditText et_path;    private ImageView iv_image;    protected static final int UPDATA = 1;    private Handler handler = new Handler() {        @Override        public void handleMessage(Message msg) {            switch (msg.what) {                case UPDATA:                    Bitmap bitmap = (Bitmap) msg.obj;                    iv_image.setImageBitmap(bitmap);                    break;                default:                    break;            }        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        et_path = (EditText) findViewById(R.id.et_path);        iv_image = (ImageView) findViewById(R.id.iv_image);    }    public void click(View view) {        final String path = et_path.getText().toString().trim();        if (TextUtils.isEmpty(path)) {            Toast.makeText(this, "图片路径不能为空", Toast.LENGTH_SHORT).show();        } else {            new Thread() {                @Override                public void run() {                    try {                        URL url = new URL(path);                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();                        connection.setRequestMethod("GET");                        connection.setConnectTimeout(5000);                        int code = connection.getResponseCode();                        if (code == 200) {                            InputStream is = connection.getInputStream();                            Bitmap bitmap = BitmapFactory.decodeStream(is);                            Message message = new Message();                            message.what = UPDATA;                            message.obj = bitmap;                            handler.sendMessage(message);                        } else {                            Toast.makeText(MainActivity.this, "图片错误", Toast.LENGTH_SHORT).show();                        }                    } catch (MalformedURLException e) {                        e.printStackTrace();                    } catch (IOException e) {                        e.printStackTrace();                    }                };            }.start();        }    }}
3.清单文件

添加:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
允许请求网络。