我如何从云存储下载图像,如 Whatsapp
How can i download images like Whatsapp from cloud storage
我想从云存储中下载像 WhatsApp 这样的图像,例如当用户在 Recyclerview 中时,他看到的图像质量很差,但是当他点击图像时,它会以全分辨率下载,目前我正在做的是使用 glide library 来下载图片。
目前我正在做的是,它会以全分辨率下载 recyclerview 中的图像。
if (user.getImageURL().equals("default")) {
profile_image.setImageResource(R.drawable.user2);
} else {
//and this
Glide.with(getApplicationContext()).load(user.getImageURL()).into(profile_image);
}
使用 Glide 的 CustomTarget 传递图像视图的尺寸,以便 Glide 可以将图像缩小到指定的大小。
Glide.with(this)
.load(IMAGE_URL)
.into(object : CustomTarget<Drawable>(targetImageWidth, targetImageHeight) {
override fun onLoadCleared(placeholder: Drawable?) {
// called when imageView is cleared. If you are referencing the bitmap
// somewhere else too other than this imageView clear it here
}
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
image.setImageDrawable(resource)
}
})
但是如果你只知道目标容器的一个维度而不知道图像的纵横比怎么办?您是否被迫使用原始图像尺寸?事实证明,可能有一种方法可以解决这个问题。在这种情况下,只需将您不知道的其他维度设置为 1,Glide 就会自动缩小您的图像。
imageView.viewTreeObserver.addOnGlobalLayoutListener {
Glide.with(this)
.load(TargetActivity.IMAGE_URL)
.into(object : CustomTarget<Drawable>(imageView.width, 1) {
// imageView width is 1080, height is set to wrap_content
override fun onLoadCleared(placeholder: Drawable?) {
// clear resources
}
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
// bitmap will have size 1080 x 805 (original: 1571 x 1171)
imageView.setImageDrawable(resource)
}
})
}
最简单的方法是在用户上传图像时创建图像的低质量版本并首先显示低质量图像。
在上传时创建一个 firebase 云函数触发器,它将获取上传的文件并创建它的低质量版本并将其上传到存储。 here
给出了一个官方样本
仅将低质量图像加载到 recyclerview 中的图像中。当用户点击图像时,以全屏显示并使用 glide 加载完整图像并设置 glide 的缩略图选项以加载低质量图像,直到从互联网加载完整图像。使用 Glide 4.11 它将看起来像这样-
Glide.with(view)
.load(fullImageReference)
.thumbnail(
Glide.with(view)
.load(lowImageReference)
.into(imageView)
我想从云存储中下载像 WhatsApp 这样的图像,例如当用户在 Recyclerview 中时,他看到的图像质量很差,但是当他点击图像时,它会以全分辨率下载,目前我正在做的是使用 glide library 来下载图片。
目前我正在做的是,它会以全分辨率下载 recyclerview 中的图像。
if (user.getImageURL().equals("default")) {
profile_image.setImageResource(R.drawable.user2);
} else {
//and this
Glide.with(getApplicationContext()).load(user.getImageURL()).into(profile_image);
}
使用 Glide 的 CustomTarget 传递图像视图的尺寸,以便 Glide 可以将图像缩小到指定的大小。
Glide.with(this)
.load(IMAGE_URL)
.into(object : CustomTarget<Drawable>(targetImageWidth, targetImageHeight) {
override fun onLoadCleared(placeholder: Drawable?) {
// called when imageView is cleared. If you are referencing the bitmap
// somewhere else too other than this imageView clear it here
}
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
image.setImageDrawable(resource)
}
})
但是如果你只知道目标容器的一个维度而不知道图像的纵横比怎么办?您是否被迫使用原始图像尺寸?事实证明,可能有一种方法可以解决这个问题。在这种情况下,只需将您不知道的其他维度设置为 1,Glide 就会自动缩小您的图像。
imageView.viewTreeObserver.addOnGlobalLayoutListener {
Glide.with(this)
.load(TargetActivity.IMAGE_URL)
.into(object : CustomTarget<Drawable>(imageView.width, 1) {
// imageView width is 1080, height is set to wrap_content
override fun onLoadCleared(placeholder: Drawable?) {
// clear resources
}
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
// bitmap will have size 1080 x 805 (original: 1571 x 1171)
imageView.setImageDrawable(resource)
}
})
}
最简单的方法是在用户上传图像时创建图像的低质量版本并首先显示低质量图像。
在上传时创建一个 firebase 云函数触发器,它将获取上传的文件并创建它的低质量版本并将其上传到存储。 here
给出了一个官方样本
仅将低质量图像加载到 recyclerview 中的图像中。当用户点击图像时,以全屏显示并使用 glide 加载完整图像并设置 glide 的缩略图选项以加载低质量图像,直到从互联网加载完整图像。使用 Glide 4.11 它将看起来像这样-
Glide.with(view)
.load(fullImageReference)
.thumbnail(
Glide.with(view)
.load(lowImageReference)
.into(imageView)