【发布时间】:2019-07-24 14:36:44
【问题描述】:
我有一个简单的 JSON 文件,我想下载该文件并将其保存在 内部 存储中。我的函数可以下载文件,但我无法将其保存在内部存储中:
val request = DownloadManager.Request(Uri.parse("Some_URL"))
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
request.setTitle("Download")
request.setDescription("File is downloading...")
request.allowScanningByMediaScanner()
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "FILE_NAME")
val manager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
manager.enqueue(request)
我在下载后尝试复制文件并将其保存在内部存储中:
val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
"FILE_NAME")
val result = file.readText()
this.openFileOutput("FILE_NAME", Context.MODE_PRIVATE).use {
it.write(result.toByteArray())
}
它有效,但我不喜欢它。我的做法,真的很糟糕。
【问题讨论】: