问题描述
我正在使用 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 (将#修改为@)