I am trying to download video file from firebase to local storage here is the code that i have used to download the video file
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()
}
And below is the ERROR that i am getting
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)
I am calling the download function from viewholder recyclerview adapter
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)
}
}
And in firebase the video is uploaded just using the url id
data class VideoModel(
var url:String = ""
)
The actual video's are saved in firebase storage
And and i am getting this videos to my application from firebase firestore Database
location should not be a full URL
Well what do you use? How would we know if you dont tell?