网络图片浏览器——HttpURLConnection

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

在这个案例中运用到了线程,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>
允许请求网络。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值