Drawable image in java object

2

How can I introduce a drawable element (in this case an image) into a java object?

In this case here:

public Receta(String nombre, String elaboracion, String dificultad, String ingredientes, Drawable imagen) {
    this.nombre = nombre;
    this.elaboracion = elaboracion;
    this.dificultad = dificultad;
    this.ingredientes = ingredientes;

}
    
asked by victor96 26.10.2018 в 12:44
source

1 answer

3

If the recipes are fixed, that is to say that the images are packaged in the application (apk) in the folder res/drawable , then you can save the reference to the image with its R.drawable.<nombre> which is an int field.

public Receta(String nombre, String elaboracion, String dificultad, String ingredientes, int imagen) {
    this.nombre = nombre;
    this.elaboracion = elaboracion;
    this.dificultad = dificultad;
    this.ingredientes = ingredientes;
    this.imagen = imagen;
}

Then if, for example, you have the image R.drawable.fideos_con_tuco in res/drawable :

Receta receta = new Receta("Fideos con tuco", "Hervir el agua ...", 
                            "Muy Fácil", "Fideos, ...",
                            R.drawable.fideos_con_tuco); 

When you want to use the image, for example in an ImageView: imageView.setImageResource(receta.getImagen()); //supongo que tendrás un getImagen() en Receta.

On the other hand, if it is the case is that they are dynamic recipes, that is to say that it is the user who creates them, then the images will not be in res/drawable and the way of solving the issue is totally different, since you would have to save a reference to where the app saves the image, presumably downloaded from a server.

Because of your question, it also seems to me that your case is the first.

    
answered by 26.10.2018 / 14:32
source