我嘗試使用以下代碼將檔案下載到下載檔案夾:
private fun downloadTrack(track: Track) {
val url = track.file
val request = DownloadManager.Request(Uri.parse(url))
request.setDescription("Downloading track ${track.trackName}...")
request.setTitle(track.trackName)
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
track.trackName ".mp3"
)
val manager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val downloadId: Long = manager.enqueue(request)
registerBroadcast(downloadId)
}
在以下登錄時下載它后,我試圖檢查它是否已經存在此代碼:
private fun getTrack(track: Track) {
val file =
File(
Environment.DIRECTORY_DOWNLOADS,
track.trackName ".mp3"
)
try {
if (file.isFile) {
Toast.makeText(this, "FILE EXISTS", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "FILE NOT FOUND", Toast.LENGTH_SHORT).show()
downloadTrack(track)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
還嘗試過 file.exists() 并使用目錄。沒有任何幫助,檔案下載后找不到,盡管我在同一目錄中尋找它。請幫忙。
uj5u.com熱心網友回復:
環境.DIRECTORY_DOWNLOADS
這還不夠。
你需要 :
getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/360991.html
