public class MainActivity extends Activity
{
private EditText et_path;
private ImageView iv;
private Handler handler=new Handler(){
public void handMessage(android.os.Message msg)
{
Bitmap bitmap=(Bitmap) msg.obj;
iv.setImageBitmap(bitmap);
};
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_path=(EditText) findViewById(R.id.et_path);
iv=(ImageView) findViewById(R.id.iv);
}
@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 click(View v){
new Thread(){
public void run(){
};}.start();
try
{
String path=et_path.getText().toString().trim();
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(4000);
int code=conn.getResponseCode();
if(code==200){
InputStream in =conn.getInputStream();
Bitmap bitmap=BitmapFactory.decodeStream(in);
//iv.setImageBitmap(bitmap);
Message msg=Message.obtain();
msg.obj=bitmap;
handler.sendMessage(msg);
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/ic_launcher" />
<EditText
android:id="@+id/et_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="http://news.baidu.com/resource/img/logo_news_276_88.png?date=20150104"
android:layout_gravity="center_horizontal"
android:ems="10"
android:hint="请输入图片的路径" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:layout_gravity="center_horizontal"
android:text="浏览" />