博客
关于我
安卓Kotlin 用GET方法保存文件到filesDir
阅读量:419 次
发布时间:2019-03-05

本文共 972 字,大约阅读时间需要 3 分钟。

HttpURLConnection需要通过新建Thread以执行网络请求

        fun downloadUsingUrl(url: String?) {            Thread(Runnable {                try {                    val connection = URL(url).openConnection() as HttpURLConnection                    connection.requestMethod = "GET"                    connection.connectTimeout = 8000                    connection.readTimeout = 8000                    val avatarFile = File("$filesDir/avatar.png")                    if (avatarFile.exists()) {                        avatarFile.delete()                    } else if (!filesDir.exists()) {                        filesDir.mkdir()                    }                    filesDir.setReadable(true)                    filesDir.setWritable(true)                    connection.inputStream.buffered().copyTo(avatarFile.outputStream())                } catch (ex: Exception) {                    ex.printStackTrace()                }            }).start()        }    

转载地址:http://grfzz.baihongyu.com/

你可能感兴趣的文章
OpenResty(nginx扩展)实现防cc攻击
查看>>
openresty完美替代nginx
查看>>
Openresty框架入门详解
查看>>
OpenResty(1):openresty介绍
查看>>
OpenResty(2):OpenResty开发环境搭建
查看>>
OpenResty(3):OpenResty快速入门之安装lua
查看>>
OpenResty(4):OpenResty快速入门
查看>>
OpenResty(5):Openresty 模板渲染
查看>>
OpenSearch 使用二三事
查看>>
OpenSessionInView模式
查看>>
openshift搭建Istio企业级实战
查看>>
OpenSLL
查看>>
Openssh Openssl升级
查看>>
openssh 加固
查看>>
OPENSSH升级为7.4
查看>>
ViewPager切换滑动速度修改
查看>>
OpenSSL 引入了新的治理模式和项目,来增强社区参与和决策
查看>>
openssl内存分配,查看内存泄露
查看>>
OpenSSL创建SSL证书
查看>>
openssl在cygwin下编译错误:CPU不支持x86_64(CPU you selected does not support x86-64 instruction set )
查看>>