为什么我的kotlin功能会加载Firebase存储中的图像如此慢?

发布于 2025-02-03 21:15:46 字数 1753 浏览 2 评论 0原文

我是编程的新手,对于此应用程序,我在Android Studio中使用Kotlin。我的功能可以加载来自Firebase存储的存储图像,这两个图像均可用于所有用户,也可以使用“私有”图像。它运行良好,但加载图像大约需要20-30秒。我认为它应该能够更快地修复?

这就是函数今天的外观:

private fun listStoredImages() = CoroutineScope(Dispatchers.IO).launch {
        try {
            val userImages = imageRef.child(uid).listAll().await()
            val publicImages = imageRef.child("UploadedPictures").listAll().await()

            // Loads private images from storage
            for (image in userImages.items) {
                val url = image.downloadUrl.await()
                userImageUrl.add(url.toString())

            }   // loads public images from storage
            for (publicImage in publicImages.items) {
                val publicUrl = publicImage.downloadUrl.await()
                userImageUrl.add(publicUrl.toString())
            }
            withContext(Dispatchers.Main) {
                recyclerView.adapter?.notifyDataSetChanged()
            }
        } catch (e: Exception) {
            withContext(Dispatchers.Main) {
                Toast.makeText(this@UserCreateAndEditActivity, "Error", Toast.LENGTH_SHORT).show()
            }
        }
    }

Imageref的声明:

val imageRef = Firebase.storage.reference

我不知道它是否有任何区别,但我正在使用网格显示图像。像这样:

private var gridLayoutManager: GridLayoutManager? = null

在创建中:

gridLayoutManager =
            GridLayoutManager(applicationContext, 3, LinearLayoutManager.VERTICAL, false)
        recyclerView.setHasFixedSize(true)
        imageAdapter = ImageAdapter(this, userImageUrl)
        recyclerView.layoutManager = gridLayoutManager
        recyclerView.adapter = imageAdapter

我知道如何使它更快?

I am quite new to programming and for this app I am using Kotlin in Android Studio. I have a function that loads stored images from firebase storage, both images that are available to all users and also "private" images. It works fine but it takes about 20-30 seconds to load images. I think it should be able to fix faster?

This is how the function looks today:

private fun listStoredImages() = CoroutineScope(Dispatchers.IO).launch {
        try {
            val userImages = imageRef.child(uid).listAll().await()
            val publicImages = imageRef.child("UploadedPictures").listAll().await()

            // Loads private images from storage
            for (image in userImages.items) {
                val url = image.downloadUrl.await()
                userImageUrl.add(url.toString())

            }   // loads public images from storage
            for (publicImage in publicImages.items) {
                val publicUrl = publicImage.downloadUrl.await()
                userImageUrl.add(publicUrl.toString())
            }
            withContext(Dispatchers.Main) {
                recyclerView.adapter?.notifyDataSetChanged()
            }
        } catch (e: Exception) {
            withContext(Dispatchers.Main) {
                Toast.makeText(this@UserCreateAndEditActivity, "Error", Toast.LENGTH_SHORT).show()
            }
        }
    }

The declaration of imageRef:

val imageRef = Firebase.storage.reference

I don't know if it makes any difference but I am using grid to show the images. Like this:

private var gridLayoutManager: GridLayoutManager? = null

on create:

gridLayoutManager =
            GridLayoutManager(applicationContext, 3, LinearLayoutManager.VERTICAL, false)
        recyclerView.setHasFixedSize(true)
        imageAdapter = ImageAdapter(this, userImageUrl)
        recyclerView.layoutManager = gridLayoutManager
        recyclerView.adapter = imageAdapter

Any idea how I can make it faster?

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

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

发布评论

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

评论(1

潇烟暮雨 2025-02-10 21:15:46

我只会更新视图

withContext(Dispatchers.Main) {
                recyclerView.adapter?.notifyDataSetChanged()
            }

问题在于,在所有图像URL上完成加载后, 。
在我加载每个图像后,我在for循环中添加了notifydatasethered之后,它更快了。

The problem was that I was only updating my View with

withContext(Dispatchers.Main) {
                recyclerView.adapter?.notifyDataSetChanged()
            }

after all the image urls where done loading.
After I added the notifyDataSetChanged in the for loops after each image loaded it was much quicker.

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