博客
关于我
安卓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/

你可能感兴趣的文章
python基于flask搭建http服务(二)—— 实现Excel上传、数据清洗、入库
查看>>
python基于flask搭建http服务(三)—— 使用gunicorn部署项目
查看>>
python基于flask搭建http服务(一)—— 实现数据查询和Excel导出
查看>>
Python块键盘/鼠标输入
查看>>
Python在正确的时区获取当前时间
查看>>
python在心理学研究中的应用有哪些_心理学在线研究可用平台简介和应用进展
查看>>
python系列【仅供参考】:python tornado 集成redis消息订阅的异步任务之后tornado主程序无法启动,解决方案
查看>>
Python在列表理解中使用枚举
查看>>
python在使用HTMLTestRunner时,报告为空,错误提示<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf_8'>...
查看>>
python在gpu上运行_【python】python开启GPU加速
查看>>
Python在def函数中使用for循环
查看>>