博客
关于我
安卓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实现kadanes卡达内斯算法(附完整源码)
查看>>
Objective-C实现kahns algorithm卡恩算法(附完整源码)
查看>>
Objective-C实现karatsuba大数相乘算法(附完整源码)
查看>>
Objective-C实现karger算法(附完整源码)
查看>>
Objective-C实现KMP搜索算法(附完整源码)
查看>>
Objective-C实现Knapsack problem背包问题算法(附完整源码)
查看>>
Objective-C实现knapsack背包问题算法(附完整源码)
查看>>
Objective-C实现knapsack背包问题算法(附完整源码)
查看>>
Objective-C实现knight tour骑士之旅算法(附完整源码)
查看>>
Objective-C实现knight Tour骑士之旅算法(附完整源码)
查看>>
Objective-C实现KnightTour骑士巡回赛问题算法(附完整源码)
查看>>
Objective-C实现KNN算法(附完整源码)
查看>>
Objective-C实现KNN算法(附完整源码)
查看>>
Objective-C实现KNN算法(附完整源码)
查看>>
Objective-C实现knuth morris pratt(KMP)算法(附完整源码)
查看>>
Objective-C实现knuth-morris-pratt(KMP)算法(附完整源码)
查看>>
Objective-C实现Koch snowflake科赫雪花曲线算法(附完整源码)
查看>>
Objective-C实现koch snowflake科赫雪花算法(附完整源码)
查看>>
Objective-C实现KPCA(附完整源码)
查看>>
Objective-C实现KruskalMST最小生成树的算法(附完整源码)
查看>>