我在我的 Android 应用程序中使用 firebase 存储来存储用户上传的图像。所有上传的图像都是正方形的。我发现下载这些图像会消耗大量的用户带宽,我想通过减小下载到的图像的大小来减少这种消耗方形图像视图
我正在使用 Glide 下载此图像,并且我尝试下载自定义尺寸的图像,但图像没有出现在图像视图上。
界面
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;
}
}
Finally
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 的结果
在职的!图像正在出现但是就像下载整个图像一样,消耗大量带宽。我只想下载一个自定义尺寸的图像,例如 200px x 200px。
如何执行或更改上述解决方案 1 以从 Firebase 存储下载自定义尺寸的图像?
EDIT
我尝试访问我的一张图片https://firebasestorage.googleapis.com/....m.png
从浏览器中,它已成功加载到网页。但是当我尝试将特定参数设置为我的图像网址链接时https://firebasestorage.googleapis.com/....m.png?w=100&h=100
网页上出现错误
{
"error": {
"code": 403,
"message": "Permission denied. Could not perform this operation"
}
}