如何从 firebase 存储下载特定大小的图像
How to download images with specific size from firebase storage
我在 android 应用程序中使用 firebase 存储来存储用户上传的图片。 所有上传的图片都是正方形的。
我发现下载这些图像会占用大量用户带宽,我想通过减小下载到 square imageview
中的图像大小来减少这种消耗
我正在使用 Glide 下载此图片,我已尝试下载自定义尺寸的图片,但图片未显示在图片视图中。
界面
public interface MyDataModel {
public String buildUrl(int width, int height);
}
my class 扩展了 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);
}
}
class 实现了 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 的结果
工作!图片正在显示,但 好像整个图片都已下载,这会消耗大量带宽。我只想下载自定义尺寸的图像,例如 200px x 200px。
我如何做或更改上面的解决方案 1 以从 firebase 存储下载自定义大小的图像?
编辑
我尝试从浏览器访问我的一张图片 https://firebasestorage.googleapis.com/....m.png
,它已成功加载到网页上。但是当我尝试为我的图像设置特定尺寸的参数时 url link https://firebasestorage.googleapis.com/....m.png?w=100&h=100
网页上出现错误
{
"error": {
"code": 403,
"message": "Permission denied. Could not perform this operation"
}
}
尝试使用以下命令下载图像:-
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
}
});
我终于可以使用 MyUrlLoader
class
从 firebase 存储下载图像
你看,firebase 存储 url 看起来像这样
firebasestorage.googleapis.com/XXXX.appspot.com/Folder%2Image.png?&alt=media&token=XXX
正如你在上面看到的,link已经有了这个特殊的问号字符?
代表查询字符串的开始所以当我使用 CustomImageSize
class,添加了另一个 ?
,因此 link 以两个 ?
结束,导致下载失败
firebasestorage.googleapis.com/XXXX.appspot.com/Folder%2Image.png?&alt=media&token=XXX?w=200&h=200
解决方案 是删除我的 CustomImageSize
class 中的 ?
。所以结果是这样的
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)
我在 android 应用程序中使用 firebase 存储来存储用户上传的图片。 所有上传的图片都是正方形的。 我发现下载这些图像会占用大量用户带宽,我想通过减小下载到 square imageview
中的图像大小来减少这种消耗我正在使用 Glide 下载此图片,我已尝试下载自定义尺寸的图片,但图片未显示在图片视图中。
界面
public interface MyDataModel {
public String buildUrl(int width, int height);
}
my class 扩展了 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);
}
}
class 实现了 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 的结果
工作!图片正在显示,但 好像整个图片都已下载,这会消耗大量带宽。我只想下载自定义尺寸的图像,例如 200px x 200px。
我如何做或更改上面的解决方案 1 以从 firebase 存储下载自定义大小的图像?
编辑
我尝试从浏览器访问我的一张图片 https://firebasestorage.googleapis.com/....m.png
,它已成功加载到网页上。但是当我尝试为我的图像设置特定尺寸的参数时 url link https://firebasestorage.googleapis.com/....m.png?w=100&h=100
网页上出现错误
{
"error": {
"code": 403,
"message": "Permission denied. Could not perform this operation"
}
}
尝试使用以下命令下载图像:-
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
}
});
我终于可以使用 MyUrlLoader
class
你看,firebase 存储 url 看起来像这样
firebasestorage.googleapis.com/XXXX.appspot.com/Folder%2Image.png?&alt=media&token=XXX
正如你在上面看到的,link已经有了这个特殊的问号字符?
代表查询字符串的开始所以当我使用 CustomImageSize
class,添加了另一个 ?
,因此 link 以两个 ?
结束,导致下载失败
firebasestorage.googleapis.com/XXXX.appspot.com/Folder%2Image.png?&alt=media&token=XXX?w=200&h=200
解决方案 是删除我的 CustomImageSize
class 中的 ?
。所以结果是这样的
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)