1.创建程序
创建一个“网络图片浏览器”应用程序,将包名改为cn.edu.bzu;
2.主界面布局文件
文件由一个图片控件(ImageView)和编辑框(EditText)和一个按钮(Button)组成;这个布局中用到了android:layout_weight这一属性,它不是平时权重的意思,而是代表控件渲染的优先级,值越大,控件渲染的优先级越低。
代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" 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" android:background="#aaa" tools:context="cn.edu.bzu.imagebrower.MainActivity" android:weightSum="1"> <ImageView android:layout_weight="1000" android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="200dp" app:srcCompat="@mipmap/ic_launcher" android:layout_gravity="center" /> <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="150dp" android:hint="请输入图片的路径:" android:textSize="30dp" > </EditText> <Button android:id="@+id/bt" android:text="浏览" android:onClick="click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textSize="28dp" /> </LinearLayout>
3.界面交互代码
其中创建一个URL对象,然后通过URL对象去获取HttpURLConnection对象,设置请求方法、超时时间、请求头信息,最后获取到了服务器返回的输出流,实现了获取网络上图片的功能。
package cn.edu.bzu.imagebrower; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class MainActivity extends AppCompatActivity { private EditText et; private ImageView iv; protected static final int CHANGE_UI = 1; protected static final int ERROR = 2; //主线程创建消息处理器 private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { if(msg.what == CHANGE_UI){ Bitmap bitmap = (Bitmap) msg.obj; iv.setImageBitmap(bitmap); }else if(msg.what == ERROR){ Toast.makeText(MainActivity.this, "显示图片错误", 0).show(); } }; }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et = (EditText) findViewById(R.id.et); iv = (ImageView) findViewById(R.id.iv); } public void click(View view) { final String path = et.getText().toString().trim(); if (TextUtils.isEmpty(path)) { Toast.makeText(this, "图片路径不能为空", Toast.LENGTH_SHORT).show(); } else { //子线程请求网络,Android 4.0以后访问网络不能放在主线程中 new Thread() { private HttpURLConnection conn; private Bitmap bitmap; public void run() { try { URL url = new URL(path); conn = (HttpURLConnection) url.openConnection();//发送http请求 conn.setRequestMethod("GET");//请求方式 conn.setConnectTimeout(5000);//设置超时时间 int responsecode = conn.getResponseCode();//得到服务器返回的响应码 System.out.println(responsecode); if (responsecode == 200) { InputStream is = conn.getInputStream();//获取输入流 bitmap = BitmapFactory.decodeStream(is);//将流转换成Bitmap对象 iv.setImageBitmap(bitmap); Message msg = new Message(); msg.what = CHANGE_UI; msg.obj = bitmap; handler.sendMessage(msg); } else { Toast.makeText(MainActivity.this, "显示图片失败", Toast.LENGTH_SHORT).show(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); Message msg = new Message(); msg.what = ERROR; handler.sendMessage(msg); } } ; }.start(); } } }
4.运行项目浏览图片 (1)布局文件显示的界面
(2)在文本框中输入一个网络中图片的地址
(3)单击“浏览”按钮,显示图片
![]()