java.lang.illegalargumentException:位置不应是完整的URL

发布于 2025-01-27 18:08:37 字数 3262 浏览 3 评论 0原文

我正在尝试下载从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)

我正在从View Holdeholder RecyClerview Adapter和Firebase中调用下载函数,

 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数据库

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉世弥音 2025-02-03 18:08:37

要获得基于视频下载URL的storagereference,您应该使用refererencefromurl 而不是getReference

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

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

final storageRef = FirebaseStorage.instance.refererenceFromUrl(videoUrl);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文