android 下载图片到本地并显示

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    protected Button butLoading;
    protected Button butDisplay;
    protected ImageView ima;
    private URL murl;
    Handler handler;
    private static final String MIMG = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1499753534636&di=2599426532d38becb0406764cf463f0f&imgtype=0&src=http%3A%2F%2Fdynamic-image.yesky.com%2F1080x-%2FuploadImages%2F2015%2F290%2F49%2F9ORK4DGNMWC7.jpg";
    private HttpImgThread httpImgThread;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
//主线程收到通知更新UI
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what==1){
                    ima.setImageBitmap((Bitmap) msg.obj);
                }
            }
        };
        initView();
    }
   //点击下载并显示图片
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.but_loading) {
            imaLoading();
        } 
    }
    private void initView() {
        butLoading = (Button) findViewById(R.id.but_loading);
        butLoading.setOnClickListener(MainActivity.this);
        butDisplay = (Button) findViewById(R.id.but_display);
        butDisplay.setOnClickListener(MainActivity.this);
        ima = (ImageView) findViewById(R.id.ima);
    }
    //下载图片
    private void imaLoading() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                URL url = null;
                HttpURLConnection con = null;
                try {
                    // 构造URL
                    url = new URL(MIMG);
                    // 打开连接
                    con = (HttpURLConnection) url.openConnection();
                    //请求方式
                    con.setRequestMethod("GET");
                    //设置超时时间
                    con.setReadTimeout(5000);
                    // 设置是否从httpUrlConnection读入,默认情况下是true(可以不写);
                    con.setDoInput(true);
                    //InputStream in = con.getInputStream();
                    //存放路劲
                    String parent = Environment.getExternalStorageDirectory()+"/yu/";
                    File file1 = new File(parent);
                    //不存在创建
                    if(!file1.exists()){
                        file1.mkdir();
                    }
                    File file = new File(file1, String.valueOf(System.currentTimeMillis()));
                    // 输出的文件流
                    FileOutputStream fos = new FileOutputStream(file);
                    // 输入流
                    InputStream in = con.getInputStream();
                    // 2K的数据缓冲
                    byte ch[] = new byte[2 * 1024];
                    // 读取到的数据长度
                    int len;
                    if (fos != null) {
                        // 开始读取
                        while ((len = in.read(ch)) != -1) {
                            fos.write(ch, 0, len);
                        }
                        // 完毕,关闭所有链接
                        in.close();
                        fos.close();
                    }
                    //根据本地绝对路径获取文件file.getAbsolutePath()
                    final Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                    //通知主线程更新UI
                    Message obtain = Message.obtain();
                    obtain.obj=bitmap;
                    obtain.what=1;
                    handler.sendMessage(obtain);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }).start();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值