Hirose
Hirose

Reputation: 105

Firebase - getting image URL

I'm trying to show the image, that I've saved in Firebase database.

When I try to read my image URL, it returns null. Name can be read, because it shows name..

Why is it happening? And how to get image URL saved in Firebase database?

enter image description here

Main Activity:

public class MyPetListActivity extends AppCompatActivity {

    private static String TAG = "MyPetListActivity";


    FirebaseUser firebaseUser;
    DatabaseReference reference;
    Button petadd;
    List<Pet> lsPet;

    ImageView backTo; // layout : activity_my_pet_list.xml


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_pet_list);

        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

        petadd = findViewById(R.id.petadd);
        backTo = findViewById(R.id.backTo);  //activity_my_pet_list뒤로 가기 버튼

        lsPet = new ArrayList<>();


        reference = FirebaseDatabase.getInstance().getReference("Pets").child(firebaseUser.getUid());
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot childSnapshot : dataSnapshot.getChildren())
                {
                    String key = childSnapshot.getKey();
                    Log.d(TAG,key+"키키키");
                    Pet pet = childSnapshot.getValue(Pet.class);

                    pet.getPetname();
                    pet.getPetimagerul();

                    Log.wtf(TAG, "did you get name?"+pet.getPetname());
                    Log.wtf(TAG, "did u get image url?"+pet.getPetimagerul());
                    lsPet.add(pet);
                }

                RecyclerView recyclerview_dogs = (RecyclerView) findViewById(R.id.recyclerview_dogs);
                PetAdapter petAdapter = new PetAdapter(MyPetListActivity.this,lsPet);
                recyclerview_dogs.setLayoutManager(new GridLayoutManager(MyPetListActivity.this,3));
                recyclerview_dogs.setAdapter(petAdapter);

            }


            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.wtf(TAG, "onCancelled: 펫이미지 가져오는데 문제 발생! ");
            }
        });

Log:

2019-06-27 14:54:15.291 5993-5993/com.example.blogapp E/MyPetListActivity: did u get image url?null
2019-06-27 14:54:15.295 5993-5993/com.example.blogapp W/ClassMapper: No setter/field for petimageurl found on class com.example.together.Model.Pet
2019-06-27 14:54:15.295 5993-5993/com.example.blogapp E/MyPetListActivity: did you get name?SmileDog
2019-06-27 14:54:15.298 5993-5993/com.example.blogapp E/MyPetListActivity: did u get image url?null
2019-06-27 14:54:15.301 5993-5993/com.example.blogapp W/ClassMapper: No setter/field for petimageurl found on class com.example.together.Model.Pet
2019-06-27 14:54:15.301 5993-5993/com.example.blogapp E/MyPetListActivity: did you get name?NullDog
2019-06-27 14:54:15.317 5993-5993/com.example.blogapp E/MyPetListActivity: did u get image url?null
2019-06-27 14:54:15.319 5993-5993/com.example.blogapp W/ClassMapper: No setter/field for petimageurl found on class com.example.together.Model.Pet
2019-06-27 14:54:15.319 5993-5993/com.example.blogapp E/MyPetListActivity: did you get name?Cute Dog ~
2019-06-27 14:54:15.327 5993-5993/com.example.blogapp E/MyPetListActivity: did u get image url?null

Model:

package com.example.together.Model;

public class Pet {

    private String petimageurl;
    private String petname;
    private String petbreed;
    private String petweight;
    private String birthday;
    private String gender;
    private String intro;
    private String petid;



    public Pet(){

    }

    public Pet(String petimageurl, String petname){
        this.petimageurl = petimageurl;
        this.petname = petname;

    }


    public Pet(String petimageurl, String petname, String intro){
        this.petimageurl = petimageurl;
        this.petname = petname;
        this.intro = intro;

    }


    public Pet(String petimageurl, String petname, String petbreed, String petweight, String birthday, String gender, String intro, String petid) {
        this.petimageurl = petimageurl;
        this.petname = petname;
        this.petbreed = petbreed;
        this.petweight = petweight;
        this.birthday = birthday;
        this.gender = gender;
        this.intro = intro;
        this.petid = petid;
    }


    public String getPetid() {
        return petid;
    }

    public void setPetid(String petid) {
        this.petid = petid;
    }

    public String getPetimagerul() {
        return petimageurl;
    }

    public void setPetimageurl(String petimage) {
        this.petimageurl = petimageurl;
    }

    public String getPetname() {
        return petname;
    }

    public void setPetname(String petname) {
        this.petname = petname;
    }

    public String getPetbreed() {
        return petbreed;
    }

    public void setPetbreed(String petbreed) {
        this.petbreed = petbreed;
    }

    public String getPetweight() {
        return petweight;
    }

    public void setPetweight(String petweight) {
        this.petweight = petweight;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getIntro() {
        return intro;
    }

    public void setIntro(String intro) {
        this.intro = intro;
    }
}

Upvotes: 0

Views: 392

Answers (4)

Shahbaz Hashmi
Shahbaz Hashmi

Reputation: 2815

You have typo in the setter method

public void setPetimageurl(String petimage) {
    this.petimageurl = petimageurl; // typo
}

To avoid such situation use @PropertyName with public field and don't use getter setter or use @PropertyName with getter setter as well.

@PropertyName("petimageurl")
public String petimageurl;

Upvotes: 1

Shivam Oberoi
Shivam Oberoi

Reputation: 1445

Change this-

 public void setPetimageurl(String petimage) {
        this.petimageurl = petimageurl;
    }

to-

public void setPetimageurl(String petimageurl) {
        this.petimageurl = petimageurl;
    }

Upvotes: 1

sanoJ
sanoJ

Reputation: 3128

You have typo in the setPetimageurl, change it to,

public void setPetimageurl(String petimage) {
    this.petimageurl = petimage; // you must use petimage instead petimageurl to set the value petimage to the local variable petimageurl
}

Hope this will solve your problem!

Upvotes: 1

Anupam
Anupam

Reputation: 2853

You are setting as this - 

 public void setPetimageurl(String petimage) {
        this.petimageurl = petimageurl;
    }

Make it - 

 public void setPetimageurl(String petimageurl) {
        this.petimageurl = petimageurl;
    }

Change petimage to petimageurl

Upvotes: 1

Related Questions