1. public class MainActivity extends AppCompatActivity {
  2.  
  3. private ImageView iv;
  4.  
  5. private String imageurl = "http://img06.tooopen.com/images/20161106/tooopen_sl_185050524199.jpg";
    private Bitmap bitmap;
  6.  
  7. @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  8.  
  9. iv = (ImageView) findViewById(R.id.iv_show);
  10.  
  11. findViewById(R.id.load).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    new Thread(loadrunable).start();
    }
    });
    }
  12.  
  13. private Runnable loadrunable = new Runnable() {
  14.  
  15. private InputStream is;
  16.  
  17. @Override
    public void run() {
  18.  
  19. try {
    URL imgUrl = new URL(imageurl);
    // 使用HttpURLConnection打开连接
    HttpURLConnection urlConn = (HttpURLConnection) imgUrl
    .openConnection();
    urlConn.setDoInput(true);
    urlConn.setDoOutput(false);
    urlConn.setRequestMethod("GET");
    urlConn.setConnectTimeout(3000);
    urlConn.setUseCaches(true);
    urlConn.connect();
    int code = urlConn.getResponseCode();
    Log.e("tag", "run: "+code );
    // 将得到的数据转化成InputStream
    InputStream is = urlConn.getInputStream();
    // 将InputStream转换成Bitmap
    // bitmap = getBitmapInputStream(is);
  20.  
  21. byte[] bytesInputStream = getBytesInputStream(is);
    bitmap = BitmapFactory.decodeByteArray(bytesInputStream,0,bytesInputStream.length);
  22.  
  23. Message msgone = new Message();
    msgone.what = 1;
    handler.sendMessage(msgone);
  24.  
  25. } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }finally {
  26.  
  27. if (null != is){
    try {
    is.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
    };
  28.  
  29. private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
    // super.handleMessage(msg);
    Log.e("tag", "handleMessage: "+msg.what );
  30.  
  31. if (null != bitmap && null != iv){
    iv.setImageBitmap(bitmap);
    }
    }
    };
  32.  
  33. public Bitmap getBitmapInputStream(InputStream is){
    Bitmap bp;
    bp = BitmapFactory.decodeStream(is);
  34.  
  35. return bp;
    }
  36.  
  37. public byte[] getBytesInputStream( InputStream is) throws IOException {
  38.  
  39. ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
    byte[] buff = new byte[512];
    int len;
    while ((len = is.read(buff))!= -1){
  40.  
  41. arrayOutputStream.write(buff,0,len);
    }
  42.  
  43. is.close();
    arrayOutputStream.close();
  44.  
  45. return arrayOutputStream.toByteArray();
  46.  
  47. }
    }
  48.  
  49. 重点:不要设置setDoOutput(true),post请求上传参数得设置为true;
    它默认为false: urlConn.setDoOutput(false);
  50.  
  51. 参考博客: http://blog.csdn.net/ameyume/article/details/6528205

HttpURLConnection下载图片的两种方式的更多相关文章

  1. jQuery 实现图片放大两种方式

    jQuery 实现图片放大两种方式 一.利用css样式表实现,多用于后台显示 1.这种比较简单,利用dom元素的hover实现样式切换 <style> img{ cursor: point ...

  2. UIImage加载本地图片的两种方式

    UIImage加载图片方式一般有两种: (1)imagedNamed初始化:默认加载图片成功后会内存中缓存图片,这个方法用一个指定的名字在系统缓存中查找并返回一个图片对象.如果缓存中没有找到相应的图片 ...

  3. Nodejs 传图片的两种方式

    node上传图片第一种方式 1,首先引入模块 "connect-multiparty": "~1.2.5", 在package.json中添加 "co ...

  4. UIImage创建图片的两种方式的区别

    在工作中经常会遇到添加图片,用哪种方式添加更好呢?请看详解 方法一: UIImage *image = [UIImage imageNamed:@"haha"]; 这种方法创建的图 ...

  5. android绘制圆形图片的两种方式

    看下效果先 下面有完整的示例代码 使用BitmapShader(着色器) 我们在绘制view 的时候 就是小学上美术课 用水彩笔在本子上画画 使用着色器绘制圆形图片最简单的理解方式 就是把bitmap ...

  6. tp5使用PHPWord(下载引入/composer两种方式)

    PHPWORD使用文档 一:引入 tp5.0,tp5.1: 1:composer方式(推荐) a:根目录下执行:composer require phpoffice/phpword b:引入: use ...

  7. git下载代码的两种方式以及eclipse集成git

    1.第一种使用tortoiseGit插件: 链接:https://pan.baidu.com/s/1ANDydwfaaVcUaqZDJWc_BQ 提取码:qgxt a.首先在setting中的Git中 ...

  8. button上加上图片的两种方式

    ////  ViewController.m//  UIButtonDemo////  Created by hehe on 15/9/15.//  Copyright (c) 2015年 wang. ...

  9. java 下载文件的两种方式和java文件的上传

    一:以网络的方式下载文件 try { // path是指欲下载的文件的路径. File file = new File(path); // 以流的形式下载文件. InputStream fis = n ...

随机推荐

  1. 服务器自己用户名下编译gcc

    要点: 1.上传gcc 学习命令 scp 具体格式: scp local_file remote_username@remote_ip:remote_folder scp /home/linux/so ...

  2. asp.net 发布后,遇到的导出excel报错的问题

    做的asp.net程序,最近要发布在外网上,发布过程不太难,网上都有现成的,只要按照相应的步骤基本都不会有什么问题,关键是发布成功后,程序中涉及到excel的导出或者导入问题,就会提示“检索COM 类 ...

  3. import tf

    不知道为什么,tensorflow要最后import,否则会对matplotlib的imread产生影响,产生IO错误,异常莫名其妙

  4. window.location 小结)

    其实在网上都能找到,我只是总结一下,方便自己查找使用 示例URL:http://b.a.com:88/index.php?name=kang&when=2011#first 属性     含义 ...

  5. cache是什么文件?

    通常人们所说的Cache就是指缓存SRAM. SRAM叫静态内存,"静态"指的是当我们将一笔数据写入SRAM后,除非重新写入新数据或关闭电源,否则写入的数据保持不变. 由于CPU的 ...

  6. javascript的document.execCommand(转)

    document.execCommand()方法处理html数据时常用语法格式如下: 代码: document.execCommand(sCommand[,交互方式, 动态参数]) 其 中:sComm ...

  7. Discovering the Computer Science Behind Postgres Indexes

    This is the last in a series of Postgres posts that Pat Shaughnessy wrote based on his presentation ...

  8. Win8 WinRT将替换Win32 API程序员何去何从?

    win8 新引入了称为WinRT的核心API.支持使用C/C++..NET或JavaScript来开发Metro风格的应用.这些应用自动获得硬件加速和高级电源管理的功能.现有的Silverlight和 ...

  9. Silverlight管理系统源代码(SilverlightOAFlame开发框架主要提供二次开发)

    Silverlight OA系统简介 系统功能简介 l 程序界面介绍: 左侧为主菜单,主菜单可以展开和收起,主菜单下面的所有模块都可以在数据库中扩展增加,模块的权限和用户角色挂钩,可以在数据库中创建多 ...

  10. IOS中图片拉伸技巧与方法总结(转载)

    以下内容转载自:http://my.oschina.net/u/2340880/blog/403996 IOS中图片拉伸技巧与方法总结 一.了解几个图像拉伸的函数和方法 1.直接拉伸法 简单暴力,却是 ...