如何解决Android 下载管理器无法在可移动存储设备中运行
我正在使用 downloadManager 从服务器下载大文件,我想将这些文件存储在我的可移动存储中,如 SD 卡或 USB(pendrive)。我的下载位置是 /storage/511B-14E9/Media 。我可以在我的可移动存储中成功创建 Media 文件夹。但是下载的文件没有保存在这个位置。
清单中的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
android:requestLegacyExternalStorage="true"
找到USB的方法
fun getUSB(): String? {
val storageDirectory = File("/storage")
if (!storageDirectory.exists()) {
Log.e(
"ZZZZ","getUSB: '/storage' does not exist on this device"
)
return ""
}
val files = storageDirectory.listFiles()
if (files == null) {
Log.e(
"ZZZZ","getUSB: Null when requesting directories inside '/storage'"
)
return ""
}
val possibleUSBStorageMounts: ArrayList<String> = ArrayList()
for (file in files) {
val path = file.path
if (path.contains("emulated") ||
path.contains("sdcard") ||
path.contains("self")
) {
Log.d("ZZZZ","getUSB: Found '$path' - not USB")
} else {
possibleUSBStorageMounts.add(path)
}
}
if (possibleUSBStorageMounts.size == 0) {
setErrorLog(44,"Did not find any possible USB mounts")
return ""
}
if (possibleUSBStorageMounts.size > 1) {
Log.d(
"ZZZZ","getUSB: Found multiple possible USB mount points,choosing the first one"
)
}
isUsbFound = possibleUSBStorageMounts.size != 0
if (PreferenceManager.getDefaultSharedPreferences(this)
.getString("pref_usb","") == "" || !possibleUSBStorageMounts.contains(
PreferenceManager.getDefaultSharedPreferences(this)
.getString("pref_usb","")
)
) {
PreferenceManager.getDefaultSharedPreferences(this)
.edit()
.putString("pref_usb",possibleUSBStorageMounts[0])
.apply()
} else if (possibleUSBStorageMounts.size > 1) {
possibleUSBStorageMounts.forEach { usb ->
if (usb == PreferenceManager.getDefaultSharedPreferences(this)
.getString("pref_usb","")
) {
possibleUSBStorageMounts[0] = usb
}
}
}
return possibleUSBStorageMounts[0]
}
下载视频的方法
private fun downloadFromUrl(url : String){
if (manager == null) {
manager =
getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
}
if (url != "") {
val request = DownloadManager.Request(Uri.parse(url))
request.setDescription("")
request.setTitle("Download")
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
val path = getUSB() + "/"
val dir = File(path,"Media")
if (!dir.exists()) {
dir.mkdir()
}
val file = File(dir,url.md5() + ".mp4")
val uri = Uri.fromFile(file)
request.setDestinationUri(uri)
}
我的问题是,是否可以将文件直接下载到可移动存储? .如果是,情况如何?
注意:现在我可以在 Mi Android TV 中下载文件,但在 Sony Bravia Android TV 中无法下载(访问被拒绝,无法向 USB 写入任何内容)。
提前致谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。