我正在尝试将视频文件从 firebase 下载到本地存储,这是我用来下载视频文件的代码
private fun downloadVideo(videoModel: VideoModel) {
val videoUrl = videoModel.url
val storageRef = FirebaseStorage.getInstance().getReference(videoUrl)
storageRef.metadata
.addOnSuccessListener { storageMeta ->
val fileName = storageMeta.name
val fileType = storageMeta.contentType
val fileDiroctory = Environment.DIRECTORY_DOWNLOADS
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val uri = Uri.parse(videoUrl)
val request = DownloadManager.Request(uri)
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setDestinationInExternalPublicDir(fileDiroctory,"$fileName.mp4")
downloadManager.enqueue(request)
}
.addOnFailureListener {
Toast.makeText(context,"Please try again",Toast.LENGTH_SHORT).show()
}
下面是我遇到的错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kathayat.pahadiwallpaper, PID: 3703
java.lang.IllegalArgumentException: location should not be a full URL.
at com.google.firebase.storage.FirebaseStorage.getReference(FirebaseStorage.java:317)
at com.kathayat.pahadiwallpaper.adapater.VideoAdapter.downloadVideo(VideoAdapter.kt:92)
at com.kathayat.pahadiwallpaper.adapater.VideoAdapter.onBindViewHolder$lambda-1(VideoAdapter.kt:81)
at com.kathayat.pahadiwallpaper.adapater.VideoAdapter.$r8$lambda$__m2A7Vc78QjfOEKSP6EkyLGWoE(Unknown Source:0)
at com.kathayat.pahadiwallpaper.adapater.VideoAdapter$$ExternalSyntheticLambda0.onClick(Unknown Source:4)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
我正在从viewholder recyclerview适配器调用下载功能
override fun onBindViewHolder(holder: VideoHolder, position: Int) {
val videoUrl = videoList[position]
videoList[position]?.let { holder.setVideoData(it) }
holder.downloadVideo.setOnClickListener {
if (videoUrl != null) {
downloadVideo(videoUrl)
}
}
在 firebase 中,视频仅使用 url id 上传
data class VideoModel(
var url:String = ""
)
实际视频保存在 Firebase 存储中
我正在从 firebase firestore 数据库将此视频发送到我的应用程序
要根据视频的下载 URL 获取
StorageReference
,您应该 使用 refererenceFromUrl
而不是 getReference
。
final storageRef = FirebaseStorage.instance.refererenceFromUrl(videoUrl);