Firebase-获取图像URL

时间:2019-06-27 06:02:22

标签: android firebase firebase-realtime-database

我正在尝试显示保存在Firebase数据库中的图像。

当我尝试读取图像URL时,它返回null。 可以读取名称,因为它显示名称。

为什么会这样?以及如何获取保存在Firebase数据库中的图像URL?

enter image description here

主要活动:

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: 펫이미지 가져오는데 문제 발생! ");
            }
        });

日志

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

型号:

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

4 个答案:

答案 0 :(得分:1)

You are setting as this - 

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

Make it - 

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

将petimage更改为petimageurl

答案 1 :(得分:1)

您在setPetimageurl中有错字,将其更改为

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

希望这可以解决您的问题!

答案 2 :(得分:1)

更改此内容

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

到-

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

答案 3 :(得分:1)

setter方法中有错字

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

为避免这种情况,请在公共字段中使用@PropertyName,不要同时使用getter setter或也将@PropertyName与getter setter一起使用。

@PropertyName("petimageurl")
public String petimageurl;