1

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

enter image description here

And and i am getting this videos to my application from firebase firestore Database

enter image description here

4
  • location should not be a full URL Well what do you use? How would we know if you dont tell?
    – blackapps
    Commented May 11, 2022 at 9:27
  • In firebase I have uploaded video without any id do you think that would be the issue firebase can't get the whole video URL
    – Dkathayat1
    Commented May 11, 2022 at 9:36
  • Show us the URL that you're using.
    – Alex Mamo
    Commented May 11, 2022 at 9:44
  • The actual video are saved in firebase storage and i am showing them by taking the reference from storage to firebase firestore i have edited my question and added more information
    – Dkathayat1
    Commented May 11, 2022 at 10:04

1 Answer 1

2

To get a StorageReference based on download URL for the video, you should use refererenceFromUrl instead of getReference.

final storageRef = FirebaseStorage.instance.refererenceFromUrl(videoUrl);
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.