本文介绍了如何从Firebase存储下载特定大小的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的android应用程序中使用firebase存储来存储用户上传的图片。所有上传的图片都是方形的。
我发现下载这些图片会消耗大量的用户带宽,我想通过减少下载到 squareviewview



我正在使用Glide下载此图像,并试图下载自定义大小的图像,但图像未出现在imageview上。


$ b

接口

  public interface MyDataModel {
public String buildUrl(int width,int height);
}

我扩展了BaseGlideUrlLoader的类

  public class MyUrlLoader extends BaseGlideUrlLoader< MyDataModel> {
public MyUrlLoader(Context context){
super(context);

$ b @Override
protected String getUrl(MyDataModel model,int width,int height){
//在这里构造正确大小的url。
return model.buildUrl(width,height);




$ p $实现MyDataModel接口的类

  public class CustomImageSize implements MyDataModel {

private String uri;

public CustomImageSize(String uri){
this.uri = uri;

$ b @Override
public String buildUrl(int width,int height){

return uri +?w =+ width + & h =+ height;




最后

  CustomImageSize size = new CustomImageSize(uri); 
$ b Glide.with(context)
.using(new MyUrlLoader(context))
.load(size)
.centerCrop()
.priority (Priority.HIGH)
.into(imageView);

解决方案效果



图片没有出现在我的方形图片视图中

解决方案2:使用firebase图片加载器

  //引用Firebase存储中的图像文件
StorageReference storageReference = ...;

ImageView imageView = ...;
$ b $ //使用Glide
加载图片Glide.with(this / * context * /)
.using(new FirebaseImageLoader())
.load(storageReference )
.into(imageView);

上述解决方案2的结果 $ b

工作!图像正在出现,但它像整个图像被下载,消耗大量的带宽。我只想要一个自定义大小的图像,例如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
}
}


解决方案



您看到,firebase存储网址看起来像这样

  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;

$ b @Override
public String buildUrl(int width,int height){

return uri +w =+ width +& amp; ; h =+ height;


code


虽然下载了,我不确定整个图像是否正在下载或只是自定义大小。这是因为,我试图在纠正错误导致查看失败后在浏览器中访问图像,但仍然收到整个图像。不是调整大小的图像(W = 200& H = 200)

I am using firebase storage in my android app to store images uploaded by users. all images uploaded are square in shape. I discovered that downloading this images consumes a lot of user bandwidth and i would like to reduce this consumption by reducing the size of image downloaded into a square imageview

I am using Glide to download this images and i have tried to download images of custom size but the image is not appearing on the imageview.

interface

public interface MyDataModel {
  public String buildUrl(int width, int height);
}

my class which extends 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 which implements MyDataModel interface

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);

RESULTS OF SOLUTION ABOVE

Image is not appearing in my square imageview

SOLUTION 2: use firebase image loader

    // 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);

RESULT OF SOLUTION 2 ABOVE

working! image is appearing BUT it's like entire image is been downloaded which consume a lot of bandwidth. I just want a custom size image e.g 200px by 200px to be downloaded.

How can I do or change in my solution 1 above to download images of custom size from firebase storage?

EDIT

I have tried to access one of my images https://firebasestorage.googleapis.com/....m.png from the browser and it was loaded successfully to the webpage. but when i try to put to size specific parameters to my image url link https://firebasestorage.googleapis.com/....m.png?w=100&h=100 an error appeared on the webpage

 {
  "error": {
    "code": 403,
    "message": "Permission denied. Could not perform this operation"
  }
}
解决方案

I was finally able to download images from firebase storage using MyUrlLoader class

You see, firebase storage urls look like this

firebasestorage.googleapis.com/XXXX.appspot.com/Folder%2Image.png?&alt=media&token=XXX

As you can see above, the link already have this special question mark character ? which stands for the start of querying string so when i use CustomImageSize class, another ? was being added so the link was ending up with two ? which made downloading to fail

firebasestorage.googleapis.com/XXXX.appspot.com/Folder%2Image.png?&alt=media&token=XXX?w=200&h=200

Solution was to remove the ? in my CustomImageSize class. so it ended up like this

    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;
  }
}

Although it downloaded, am not sure whether entire image was being downloaded or just the custom size one. This is because, i tried to access the image in my browser after correcting the error that was making viewing to fail, but still i was receiving an entire image. not a resized image (w=200&h=200)

这篇关于如何从Firebase存储下载特定大小的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:14