如何从firebase存储下载特定大小的图片
我在我的android应用中使用firebase存储来存储用户上传的图片。 上传的所有图片都是方形的。 我发现下载此图片消耗了大量用户的带宽,我想通过减少下载图像的大小为方的ImageView减少这种消费如何从firebase存储下载特定大小的图片
我使用滑翔下载这个图片,我有试图下载自定义大小的图像,但图像没有出现在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);
结果没有出现在我的方形的ImageView
解决方法2:用火力图像加载器
// 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的结果如上
工作!图像正在出现但是这就像整个图像被下载,这会消耗大量的带宽。我只想要一个自定义大小的图像,例如200pxx200px下载。
如何在我的解决方案1中执行或更改以从Firebase存储下载自定义大小的图像?
编辑
我曾经试图访问我的图片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"
}
}
我终于能使用MyUrlLoader
类
你看下载的火力存储图像,火力存储的URL看起来像这样
firebasestorage.googleapis.com/XXXX.appspot.com/Folder%2Image.png?&alt=media&token=XXX
正如你可以在上面看到,该链接已经有了这个特殊的问号字符?
它代表查询,所以当我使用CustomImageSize
类,另一个?
正在加入这样的链接被两个结束了串的开始?
这使得下载失败
firebasestorage.googleapis.com/XXXX.appspot.com/Folder%2Image.png?&alt=media&token=XXX?w=200&h=200
解决方案是删除?
我CustomImageSize
类。所以它弄成这个样子
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;
}
}
虽然它下载的,我不知道整个图像是否正在下载或只是自定义尺寸之一。这是因为,我试图在更正导致查看失败的错误之后在我的浏览器中访问该图像,但仍然收到整个图像。没有调整大小的图像(w = 200 & h = 200)
尝试使用以下命令下载图像的错误: -
StorageReference islandRef = storageRef.child("yourImage.jpg");
// defines the specific size of your image
final long ONE_MEGABYTE = 1024 * 1024;
islandRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
// Data for "yourImage.jpg" is returns, use this as needed
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
});
这很好,但效率不高,可能会抛出“OutOfMemoryError”,从而导致应用程序发生冲突。我想Lo使用Glide –
有效地加载自定义大小的图像,但是取代ONE_MEGABYTE常量为1024 * 1024,您可以根据您的要求减少值。 –
我不知道您可以通过传递“w”和“h”GET请求来向Firebase存储中的上传图像请求特定大小。您能否分享提及此功能的文档或文章?我在官方文档中找不到它。 – Wilik
@Wilik找到任何解决方案? – ono