我无法在我的 Android Studio 项目中从本地获取照片。 (GlideException:加载资源失败)

问题描述 投票:0回答:1

我在我的android项目中使用glide。并且图像无法加载。我收到“class com.bumptech.glide.load.engine.GlideException:无法加载资源”错误。来自互联网的照片网址是有效的。

inner class PetViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val nameText: TextView = itemView.findViewById(R.id.petNameTextView)
        private val petImage: ImageView = itemView.findViewById(R.id.petImageView)
        
        fun bind(pet: Pet) {
            nameText.text = pet.Name
            Glide.with(itemView.context)
                .load("/Users/macbookair/Desktop/evcil_hayvan_app/uploads/pet_29022024004317.jpg")
                .listener(object : RequestListener<Drawable> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Drawable>?,
                        isFirstResource: Boolean
                    ): Boolean {
                        Log.e("Glide", "Image loading failed", e)
                        return false
                    }

                    override fun onResourceReady(
                        resource: Drawable?,
                        model: Any?,
                        target: Target<Drawable>?,
                        dataSource: DataSource?,
                        isFirstResource: Boolean
                    ): Boolean {
                        //başarılı yükleme durumunda burası çalışacak
                        return false
                    }
                })
                .into(petImage)
        }
    }

登录如下:

Image loading failed
                                                                                                    class com.bumptech.glide.load.engine.GlideException: Failed to load resource
                                                                                                    There were 3 causes:
                                                                                                    java.io.FileNotFoundException(/Users/macbookair/Desktop/evcil_hayvan_app/uploads/pet_29022024004317.jpg: open failed: ENOENT (No such file or directory))
                                                                                                    java.io.FileNotFoundException(open failed: ENOENT (No such file or directory))
                                                                                                    java.io.FileNotFoundException(open failed: ENOENT (No such file or directory))
                                                                                                     call GlideException#logRootCauses(String) for more detail
                                                                                                      Cause (1 of 3): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class java.io.InputStream, LOCAL
                                                                                                    There was 1 cause:
                                                                                                    java.io.FileNotFoundException(/Users/macbookair/Desktop/evcil_hayvan_app/uploads/pet_29022024004317.jpg: open failed: ENOENT (No such file or directory))
                                                                                                     call GlideException#logRootCauses(String) for more detail
                                                                                                        Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetch failed
                                                                                                    

我无法从本地获取照片。请帮助我。

android node.js kotlin android-glide android-image
1个回答
0
投票

/Users/macbookair/Desktop/evcil_hayvan_app/uploads/pet_29022024004317.jpg
似乎是 macOS 计算机的文件系统路径。

您的代码适用于 Android 应用程序。 macOS 不运行 Android 应用程序。 Android 不使用 macOS 文件系统路径。

您的

load()
调用需要引用 Android 应用程序可以访问的内容。它无法通过文件系统路径访问您的 macOS 计算机。因此,也许可以将图像上传到 Web 服务器并使用带有
https://
load()
URL。

© www.soinside.com 2019 - 2024. All rights reserved.