Home > Mobile >  Can you Display an image that is part of firebase database on Android Studio?
Can you Display an image that is part of firebase database on Android Studio?

Time:02-27

I want to display my firebase database but I want each entry to have an image attached to it. Is it possible to have the variable type "Image" in my Exercise Class? If so how would display it when I run it on my app. I can display all the Strings but when I tried to display the images it would crash. DATABASE EXAMPLE

public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

    Exercise exercise = list.get(position);
    holder.exerciseName.setText(exercise.getExerciseName());
    holder.muscleGroupName.setText(exercise.getMuscleGroupName());
    holder.equipmentName.setText(exercise.getEquipmentName());
    holder.imageView.setImageResource(exercise.getImage());


}

public class Exercise {

String exerciseName, muscleGroupName, equipmentName;
Image image;

public Image getImage() {
    return image;
}

public String getExerciseName() {
    return exerciseName;
}

public String getMuscleGroupName() {
    return muscleGroupName;
}

public String getEquipmentName() {
    return equipmentName;
}

}What I'm trying to display

CodePudding user response:

You could hold it in the db as an array of bytes. But I really wouldn't suggest storing images in a database that way. Usually you put a URL or filename to the image, then load the image from there.

CodePudding user response:

on your build.gradle(Module: app) under dependencies add this library:

dependencies {

 
 //other dependencies 
 
 //GLide
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}

re write your class as:

public class Exercise {

        String exerciseName, muscleGroupName, equipmentName,image;

        public String getImage() {
            return image;
        }

        public String getExerciseName() {
            return exerciseName;
        }

        public String getMuscleGroupName() {
            return muscleGroupName;
        }

        public String getEquipmentName() {
            return equipmentName;
        }

    }

Use it this way:

Exercise exercise = list.get(position);
                    holder.exerciseName.setText(exercise.getExerciseName());
                    holder.muscleGroupName.setText(exercise.getMuscleGroupName());
                    holder.equipmentName.setText(exercise.getEquipmentName());
                    Glide.with(holder.imageView)
                            .load(exercise.getImage())
                            .into(holder.imageView);
  • Related