英文:
How to download any file from server on specific location in android 10?
问题
以下是翻译好的代码部分:
private fun downloadV(url: String) {
val fileName = System.currentTimeMillis().toString() + ".mp4"
val downloaduri = Uri.parse(url)
val request = DownloadManager.Request(downloaduri)
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
request.setAllowedOverRoaming(false)
request.setTitle(fileName)
request.setDescription(fileName)
request.setDestinationInExternalPublicDir("/XYZ", fileName)
request.allowScanningByMediaScanner()
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
downloadManager?.enqueue(request)
}
英文:
My code for download file from server is working fine on API level 28 and below but it is not working on API level 29.
private fun downloadV(url: String) {
val fileName = System.currentTimeMillis().toString() + ".mp4"
val downloaduri = Uri.parse(url)
val request = DownloadManager.Request(downloaduri)
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
request.setAllowedOverRoaming(false)
request.setTitle(fileName)
request.setDescription(fileName)
request.setDestinationInExternalPublicDir("/XYZ", fileName)
request.allowScanningByMediaScanner()
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
downloadManager?.enqueue(request)
}
答案1
得分: 1
将以下这行代码添加到 Application 标签下的 Manifest 文件中:
requestLegacyExternalStorage="true"
英文:
Add this line to Manifest in Application tag
requestLegacyExternalStorage="true"
答案2
得分: 0
请检查这份文档:
https://developer.android.com/training/data-storage/shared/documents-files#grant-access-directory
或者尝试在运行时授予权限访问存储:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
英文:
Check this document once
https://developer.android.com/training/data-storage/shared/documents-files#grant-access-directory
or try to grant runtime permission for storage
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论