一、注意点:连接对象获取,请求方法“GET”,资源获取超时设置,建立连接,通过连接获取输入流,采用谷歌API:BitmapFactory得到图片对象Bitmap。

public Bitmap getInternetPicture(String UrlPath) {Bitmap bm = null;// 1、确定网址// http://pic39.nipic.com/20140226/18071023_164300608000_2.jpgString urlpath = UrlPath;// 2、获取Uritry {URL uri = new URL(urlpath);// 3、获取连接对象、此时还没有建立连接HttpURLConnection connection = (HttpURLConnection) uri.openConnection();// 4、初始化连接对象// 设置请求的方法,注意大写connection.setRequestMethod("GET");// 读取超时connection.setReadTimeout(5000);// 设置连接超时connection.setConnectTimeout(5000);// 5、建立连接connection.connect();// 6、获取成功判断,获取响应码if (connection.getResponseCode() == 200) {// 7、拿到服务器返回的流,客户端请求的数据,就保存在流当中InputStream is = connection.getInputStream();// 8、从流中读取数据,构造一个图片对象GoogleAPIbm = BitmapFactory.decodeStream(is);// 9、把图片设置到UI主线程// ImageView中,获取网络资源是耗时操作需放在子线程中进行,通过创建消息发送消息给主线程刷新控件;Log.i("", "网络请求成功");} else {Log.v("tag", "网络请求失败");bm = null;}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return bm;}


二、 同时要注意网络操作需在子线程操作,以免引起主线程阻塞,影响用途体验,同时采用handler消息机制进行参数处理,刷新UI控件。

public void onClick(View v){new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubString urlpath = "http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg";Bitmap bm = getInternetPicture(urlpath);Message msg = new Message();// 把bm存入消息中,发送到主线程msg.obj = bm;handler.sendMessage(msg);}}).start();}

三、 主线程处理消息队列中的消息,并刷新相应UI控件


Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {ImageView imgView = (ImageView) findViewById(R.id.internet_imageview);imgView.setImageBitmap((Bitmap) msg.obj);};};

四、获取网络图片,采用缓存保存文件

public Bitmap getInternetPicture(String UrlPath) {Bitmap bm = null;// 1、确定网址// http://pic39.nipic.com/20140226/18071023_164300608000_2.jpgString urlpath = UrlPath;// 2、获取Uritry {URL uri = new URL(urlpath);// 3、获取连接对象、此时还没有建立连接HttpURLConnection connection = (HttpURLConnection) uri.openConnection();// 4、初始化连接对象// 设置请求的方法,注意大写connection.setRequestMethod("GET");// 读取超时connection.setReadTimeout(5000);// 设置连接超时connection.setConnectTimeout(5000);// 5、建立连接connection.connect();// 6、获取成功判断,获取响应码if (connection.getResponseCode() == 200) {// 7、拿到服务器返回的流,客户端请求的数据,就保存在流当中InputStream is = connection.getInputStream();// 8、开启文件输出流,把读取到的字节写到本地缓存文件File file = new File(getCacheDir(), getFileName(urlpath));FileOutputStream fos = new FileOutputStream(file);int len = 0;byte[] b = new byte[1024];while ((len = is.read(b)) != -1) {fos.write(b, 0, len);}fos.close();is.close();//9、 通过图片绝对路径,创建Bitmap对象bm = BitmapFactory.decodeFile(file.getAbsolutePath());Log.i("", "网络请求成功");} else {Log.v("tag", "网络请求失败");bm = null;}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return bm;}public String getFileName(String path) {int index = path.lastIndexOf("/");return path.substring(index + 1);}}





更多相关文章

  1. 浅谈Android之SurfaceFlinger相关介绍(二)
  2. Android锁屏状态下弹出activity,如新版qq的锁屏消息提示
  3. Android(安卓)初体验
  4. Android(安卓)HandlerThread源码解析
  5. Android(安卓)Okhttp3了解及封装类使用
  6. Android(安卓)网络框架初探
  7. Android(安卓)动画AlphaAnimation类方法
  8. 如何设定Activity间切换时的动画
  9. Android之智能问答机器人

随机推荐

  1. android 五大布局经典演绎
  2. Android――动画专题研究
  3. [置顶] Android常用命令
  4. 如何设置Android命令行环境变量
  5. android uiautomator自动化测试
  6. Android中的多击事件
  7. 【转】android:minSdkVersion 相关知识
  8. 自定义ListView中的分割线(转)
  9. Android开发学习之Animation之Android帧
  10. 在iPhone 和 Android设备上,HTML5的关于键