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

你可能感兴趣的文章
Objective-C实现TCP Server 多线程同时连接多个客户端(附完整源码)
查看>>
Objective-C实现TCP拥塞控制(附完整源码)
查看>>
Objective-C实现tcp网络通讯服务器+客户端(附完整源码)
查看>>
Objective-C实现temperature conversions温度转换算法(附完整源码)
查看>>
Objective-C实现Tenengrad梯度函数(附完整源码)
查看>>
Objective-C实现ternary search三元搜索算法(附完整源码)
查看>>
Objective-C实现TernarySearch三分查找算法(附完整源码)
查看>>
Objective-C实现The Game of Life 生命游戏算法(附完整源码)
查看>>
Objective-C实现tim sort排序算法(附完整源码)
查看>>
Objective-C实现Timsort算法(附完整源码)
查看>>
Objective-C实现TOPK算法(附完整源码)
查看>>
Objective-C实现topological sort拓扑排序算法(附完整源码)
查看>>
Objective-C实现topologicalSort拓扑排序算法(附完整源码)
查看>>
Objective-C实现tower of hanoi河内塔算法(附完整源码)
查看>>
Objective-C实现trapezoidal rule梯形法则算法(附完整源码)
查看>>
Objective-C实现Trapping Rain Water捕获雨水问题算法(附完整源码)
查看>>
Objective-C实现Travelling Salesman算法(附完整源码)
查看>>
Objective-C实现tree sort树排序算法(附完整源码)
查看>>
Objective-C实现trial Division试除法算法(附完整源码)
查看>>
Objective-C实现Tribonacci Number特里波纳契数算法(附完整源码)
查看>>