【发布时间】:2017-07-17 18:20:00
【问题描述】:
我在我的 android 应用程序中使用 firebase 存储来存储用户上传的图像。 所有上传的图片都是方形的。 我发现下载这些图像会消耗大量用户带宽,我想通过减小下载到 方形图像视图
的图像大小来减少这种消耗我正在使用 Glide 下载这些图像,我尝试下载自定义大小的图像,但图像没有出现在 imageview 上。
界面
public interface MyDataModel {
public String buildUrl(int width, int height);
}
我的类扩展了 BaseGlideUrlLoader
public class MyUrlLoader extends BaseGlideUrlLoader<MyDataModel> {
public MyUrlLoader(Context context) {
super(context);
}
@Override
protected String getUrl(MyDataModel model, int width, int height) {
// Construct the url for the correct size here.
return model.buildUrl(width, height);
}
}
实现 MyDataModel 接口的类
public class CustomImageSize implements MyDataModel {
private String uri;
public CustomImageSize(String uri){
this.uri = uri;
}
@Override
public String buildUrl(int width, int height) {
return uri + "?w=" + width + "&h=" + height;
}
}
终于
CustomImageSize size = new CustomImageSize(uri);
Glide.with(context)
.using(new MyUrlLoader(context))
.load(size)
.centerCrop()
.priority(Priority.HIGH)
.into(imageView);
上述解决方案的结果
图像没有出现在我的方形图像视图中
解决方案 2:使用 firebase 图像加载器
// Reference to an image file in Firebase Storage
StorageReference storageReference = ...;
ImageView imageView = ...;
// Load the image using Glide
Glide.with(this /* context */)
.using(new FirebaseImageLoader())
.load(storageReference)
.into(imageView);
上述解决方案 2 的结果
工作!图像出现了,但是就像整个图像被下载,消耗了大量的带宽。我只想下载一个自定义大小的图像,例如 200 像素 x 200 像素。
如何在上面的解决方案 1 中执行或更改以从 Firebase 存储下载自定义大小的图像?
编辑
我尝试从浏览器访问我的一张图片https://firebasestorage.googleapis.com/....m.png,它已成功加载到网页。但是当我尝试将特定参数设置为我的图片 url 链接 https://firebasestorage.googleapis.com/....m.png?w=100&h=100 时,网页上出现了一个错误
{
"error": {
"code": 403,
"message": "Permission denied. Could not perform this operation"
}
}
【问题讨论】:
-
我不知道您是否可以通过传递“w”和“h”GET 请求来为 Firebase 存储中上传的图像请求特定大小。您能否分享提及此功能的文档或文章?我在官方文档中找不到。
-
@Wilik 找到任何解决方案?
标签: android firebase firebase-storage android-glide