效果图:
MainActivity代码:
public class MainActivity extends Activity {
Button button;
ImageView imageView;
EditText ed;
private static final int MSG_SUCCESS = 0;// 获取图片成功的标识
private static final int MSG_FAILURE = 1;// 获取图片失败的标识
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findId();
}
public void findId() {
button = (Button) findViewById(R.id.btnView);
imageView = (ImageView) findViewById(R.id.ivImage);
ed = (EditText) findViewById(R.id.edt);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_SUCCESS:
Bitmap bit = (Bitmap) msg.obj;
imageView.setImageBitmap(bit);
break;
default:
break;
}
};
};
public void viewImage(View view) {
final String path = ed.getText().toString();//获取输入的图片路径
if (TextUtils.isEmpty(path)) {
Toast.makeText(MainActivity.this, " 路径不为空", Toast.LENGTH_SHORT)
.show();
} else {
new Thread() {
public void run() {
URL url;
try {
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);//写入一个bitmap流
Message m = new Message();
m.what = MSG_SUCCESS;
m.obj = bitmap;
handler.sendMessage(m);
} else {
Toast.makeText(MainActivity.this, "打开失败",
Toast.LENGTH_SHORT).show();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
}
}
activity_main.xml代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.bzu.youdaodemo.MainActivity" >
<EditText
android:id="@+id/edtWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:hint="输入查找的单词"
android:ems="10"
/>
<Button
android:id="@+id/btnFind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/edtWord"
android:layout_toRightOf="@+id/edtWord"
android:onClick="findWord"
android:text="查找" />
<WebView
android:id="@+id/webResult"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/edtWord" />
</RelativeLayout>