效果显示图如下所示:
1. UI设计
2.
访问网络从网络中获取图片代码如下:
public void showImage(View view){
String path=etUr1.getText().toString();
if(TextUtils.isEmpty(path)){
Toast.makeText(this,"图片路径不能为空",Toast.LENGTH_SHORT).show();
}
else{
//连接服务器,请求获取图片
try{
URL url=new URL(path);
//发出http请求
HttpURLConnection httpURLConnection=( HttpURLConnection) url.openConnection();
HttpURLConnection.setRequestMethod("GET");
//设置连接超时时间
HttpURLConnection.setConnectTimeout(5000);
int responsecode= HttpURLConnection.getResponseCode();
if( responsecode==200){
InputStream is=HttpURLConnection.getInputStream(is);
ivNetImage.setImageBitmap(bitmap);
}
else{
Toast.makeText(this,"显示图片失败",Toast.LENGTH_SHORT).show();
}
}catch(MalformedURLException e){
e.prntStackTrace();
}catch(IOException e){
e.prntStackTrace();}
}