如何从android10中特定位置的服务器下载任何文件?

wlp8pajw  于 2021-07-05  发布在  Java
关注(0)|答案(2)|浏览(439)

我从服务器下载文件的代码在api级别28及以下工作正常,但在api级别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)
}
ryhaxcpt

ryhaxcpt1#

检查此文档一次https://developer.android.com/training/data-storage/shared/documents-files#grant-访问目录
或者尝试授予存储的运行时权限

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

ss2ws0br2#

将此行添加到应用程序标记中的清单

requestLegacyExternalStorage="true"

相关问题