private Bitmap getUrlimg(String url) throws IOException {           Bitmap bitmap = null;           URL imageUrl = null;           String head = "http://114.80.209.134:13080/";           String tureUrl = head + url;           imageUrl = new URL(tureUrl);                      HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();           conn.connect();           InputStream is = conn.getInputStream();           BufferedInputStream bis = new BufferedInputStream(is);           bitmap =            bis.close();           is.close();             return bitmap;       }  


以前经常用这种方式获得图片,但是今天发现当图片比较多而且大时,经常 失败,
后来发现 io流的获取没有出错,得到的是完整的io流,但是 还是失败,发现是那个 BitmapFactory.decodeStream(bis);的问题 后来改成这样,见下,可以完全解决问题。

private Bitmap getUrlimg(String url) throws IOException {           Bitmap bitmap = null;           URL imageUrl = null;           String head = "http://114.80.209.134:13080/";           String tureUrl = head + url;           imageUrl = new URL(tureUrl);             HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();           conn.setRequestMethod("GET");           conn.setConnectTimeout(5 * 1000);           conn.connect();           InputStream is = conn.getInputStream();           byte[] bt = getBytes(is);           bitmap = BitmapFactory.decodeByteArray(bt, 0, bt.length);           is.close();           conn.disconnect();           return bitmap;       }              private byte[] getBytes(InputStream is) throws IOException {             ByteArrayOutputStream baos =new ByteArrayOutputStream();             byte[] b =new byte[1024];           int len = 0;           while ((len = is.read(b, 0, 1024)) !=-1) {               baos.write(b, 0, len);               baos.flush();           }           byte[] bytes = baos.toByteArray();           return bytes;       }  




转自 : http://www.cn-java.com/www1/?action-viewnews-itemid-103146

更多相关文章

  1. 在android中实现两张图片对比
  2. android图片缩放(指定大小) drawable获取图片后怎么设置图片大小
  3. Android调用相机拍照并返回路径和调用系统图库选择图片
  4. [android]简单方法记载 在adapter中异步加载网络图片
  5. android打开,保存图片到sd卡,显示图片
  6. Android Drawable和Bitmap图片之间转换
  7. android加载gif图片
  8. android从网络获取图片
  9. Android 开发图片压缩/缩略图的方法

随机推荐

  1. Android中利用Camera与Matrix实现3D效果
  2. android Java代码设置textview的字体资源
  3. 【Android开发bug】Dropping event due t
  4. Google Map Android API V1中API Key的生
  5. android input设备event处理以及hotplug
  6. android 适配器里面的按钮点击切换
  7. Android Studio使用新的Gradle构建工具配
  8. Android有趣的全透明效果--Activity及Dia
  9. Android Studio 1.0尝鲜
  10. Android studio快捷键设置为Eclipse风格