Problem not having an image in Firebase Storage

0

I have an app where companies can register and have the option to upload an image of their company to show in the profile, the problem is that if they do not upload the image to the Storage, when I perform an action in the Activity I jump the following error ...

W/Glide: Load failed for null with size [-1x-1]
         class com.bumptech.glide.load.engine.GlideException: Received null model

This error occurs because you have not yet uploaded any images to your profile

And this is the code that generates the error ...

if (item.child("Logo").exists()) {
                                Logo = item.child("Logo").getValue().toString();
                            }

                            Glide.with(getApplicationContext())
                                    .load(Logo)
                                    .apply(new RequestOptions()
                                            .placeholder(R.drawable.negociophoto)
                                            .centerCrop()
                                            .dontAnimate()
                                            .dontTransform())
                                    .into(logoempresa);

I hope you can help me, thank you very much.

    
asked by Matías Nicolás Núñez Rivas 23.08.2018 в 22:38
source

2 answers

0

You do not need an if / else, just a line of code ( .error() ), either in the Adapter or Fragment / Activity:

Glide.with(getApplicationContext())
.load(Logo)
.apply(new RequestOptions()
.placeholder(R.drawable.negociophoto)
.centerCrop()
.dontAnimate()
.dontTransform())
.error(getContext().getResources().getDrawable(R.drawable.tu_imagen))
.into(logoempresa);

With this if Glide finds the value, load the image, otherwise it would load an image that you select from drawable .

    
answered by 23.08.2018 / 23:33
source
0

The problem is that Glide tries to load a resource that does not exist.

  

class com.bumptech.glide.load.engine.GlideException: Received null   model class com.bumptech.glide.load.engine.GlideException: Failed to   load resource

In this case as an option you can validate before loading the image (variable Logo )

if(Logo != null || && !Logo.trim().equals("")){
        Glide.with(getApplicationContext())
                                    .load(Logo)
                                    .apply(new RequestOptions()
                                            .placeholder(R.drawable.negociophoto)
                                            .centerCrop()
                                            .dontAnimate()
                                            .dontTransform())
                                    .into(logoempresa);
    } 

Another option: You can show an image in case the resource to load does not exist or the value of this is null , you can use the error() method.

  

error () : Set a resource to show if a load fails.

Example loading the image R.drawable.negociophoto in case of an error in the load:

        Glide.with(getApplicationContext())
                                    .load(Logo)
                                    .apply(new RequestOptions()
                                            .placeholder(R.drawable.negociophoto)
                                            .centerCrop()
                                            .dontAnimate()
                                            .dontTransform()
                                         .error(R.drawable.negociophoto)) 
                                    .into(logoempresa);

Important to comment that Glide version 4 does not matter if the variable that contains the load resource has value null or does not exist, an error will not be generated.

    
answered by 24.08.2018 в 00:23