如何在Android 10上从服务器上的特定位置下载任何文件?

huangapple go评论70阅读模式
英文:

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);
    }

huangapple
  • 本文由 发表于 2020年9月4日 14:25:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63735902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定