为什么我的kotlin功能会加载Firebase存储中的图像如此慢?
我是编程的新手,对于此应用程序,我在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只会更新视图
问题在于,在所有图像URL上完成加载后, 。
在我加载每个图像后,我在for循环中添加了notifydatasethered之后,它更快了。
The problem was that I was only updating my View with
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.