查看网络图片
来源:互联网 发布:ubuntu如何安装matlab 编辑:程序博客网 时间:2024/06/06 03:42
1.1 任务描述
输入网络图片的地址,点击浏览按钮可以显示网络中的图片。
1.2 任务实现
1.2.1 UI设计
1.2.2 访问网络从网络中获取图片
public void showImage (View view ){ String path=etUrl.getText().toString(); if(TextUtils.isEmpty(path)){ Toast.makeText(this, "图片路径不能为空",Toast.LENGTH_SHORT).show(); }else{ //连接服务器,请求获取图片 try { URL url=new URL(path); HttpURLConnection httpURLConnection=(HttpURLConnection) url.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); }else{ Toast.makeText(this, "显示图片失败", Toast.LENGTH_SHORT).show(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
运行程序,在网络上找张图片,拷贝其路径,点击浏览按钮。
1 在Android2.2模拟器上运行是否正常?
答:不正常。出现错误
2 若在Android2.2上可以运行了,那么在Android4.0以上模拟器上运行能否正常运行呢?
答:依然不正常。出现错误如下图
1.2.3 原因分析
ANR(Application Not Responding):应用程序无响应,如果应用程序不能响应用户输入
的话,系统会显示ANR。主线程也是UI线程本身就干了很多事情,绘制界面响应事件等。如果里面再直接放入一些耗时的操作,如连接网络进行IO操作,就会阻塞主线程,带来较差的用户体验。
1.2.4 修改程序
public void showImage (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 url=new URL(path); HttpURLConnection httpURLConnection=(HttpURLConnection) url.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); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }.start(); }
再次运行程序,回答如下问题:
这时程序是否运行正常了呢?如果报错,报的什么错?什么意思?
答:不正常。报错如下图
1.2.5 Android消息处理机制
1.2.6 进一步修改程序
final String path=etImageUrl.getText().toString(); if(TextUtils.isEmpty(path)){ Toast.makeText(this, "图片路径不能为空",Toast.LENGTH_SHORT).show(); }else{ new Thread(){ public void run(){ //链接服务器,请求获取图片 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(); Bitmap bitmap=BitmapFactory.decodeStream(is); //2.告诉主线程,请帮我更新界面,内容是bitmap Message msg =new Message(); msg.what=SHOW_IMAGE; msg.obj=bitmap; handler.sendMessage(msg); ivImage.setImageBitmap(bitmap); }else{ Toast.makeText(HandlerNetImageBrowserActivity.this, "显示图片失败",Toast.LENGTH_SHORT).show(); } }catch(MalformedURLException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } }; }.start(); }
运行成功如下图:
0 0
- 网络图片查看
- 网络图片查看器
- 网络图片查看器
- 查看网络图片
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- *******************网络图片查看器*************
- 网络图片查看器
- 网络图片查看器
- Android查看网络图片
- 网络图片查看器
- 网络图片查看
- android查看网络图片
- 网络图片查看
- 查看网络图片
- 查看网络图片
- 查看网络图片
- OJ第三批——Problem E:C++习题 输入输出--私有继承
- Native method not found: com.autonavi.amap.mapcore.MapCore.nativeNewInstance
- snmp agent 表格实现(子代理方式实现)
- Excel中的資料 匯入到DB
- 怎样确定 Org 所用的 Ledger 是什么
- 查看网络图片
- 使用POI中的XSSFWorkbook操作excel2007(xlsx)的异常:找不到类解决
- MySQL导入sql脚本错误:2006 - MySQL server has gone away
- 《猜猜看》之界面设置
- Linux下ntpdate时间同步
- Java基础总结之异常
- chm文件打开是空白的
- OJ第三批——Problem F:C++习题 输入输出--保护继承
- 自定义table