MainActivity.java
package com.example.imagebrowser;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText etUrl;
private ImageView ivImageShow;
public static final int SHOWIMAGE=1;
private Handler handler=new Handler(){
public void handleMessage(Message msg){
switch (msg.what) {
case SHOWIMAGE:
Bitmap bitmap=(Bitmap)msg.obj;
ivImageShow.setImageBitmap(bitmap);
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews(){
etUrl=(EditText) findViewById(R.id.etImageUrl);
ivImageShow=(ImageView)findViewById(R.id.ivImageShow);
}
@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;
}
public void viewImage(View view){
final String path=etUrl.getText().toString();
if(TextUtils.isEmpty(path)){
Toast.makeText(this,"图片路径不能为空",Toast.LENGTH_SHORT).show();
}else{
new Thread(){
public void run(){
try{
URL ur1=new URL(path);
HttpURLConnection httpURLConnection=(HttpURLConnection)ur1.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(5000);
int responsecode=httpURLConnection.getResponseCode();
if(responsecode==200){
InputStream is=httpURLConnection.getInputStream();
Bitmap bitmap=BitmapFactory.decodeStream(is);
//ivImageShow.setImageBitmap(bitmap);
Message essage=new Message();
essage.what=SHOWIMAGE;
essage.obj=bitmap;
handler.sendMessage(essage);
}else{
Toast.makeText(MainActivity.this, "失败!", Toast.LENGTH_LONG).show();
}
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
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=".MainActivity" >
<EditText
android:id="@+id/etImageUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btnView"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/ivImageShow"
android:layout_marginBottom="18dp"
android:ems="10"
android:hint="请输入图片的路径"
android:text="http://www.aomy.com/attach/2015-01/14211099284MPB.jpg" />
<Button
android:id="@+id/btnView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="viewImage"
android:text="浏览" />
<ImageView
android:id="@+id/ivImageShow"
android:layout_width="fill_parent"
android:layout_height="260dp"
android:layout_above="@+id/etImageUrl"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>