How to show image with JSONObject PHP MySQL in Android Studio?

1
 public String getStringImage(int position){
    String imagen = "";
    byte[] encodeByte = Base64.decode(Imagen, Base64.DEFAULT);
    Bitmap bitmap= BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
    try{
        JSONObject json = result.getJSONObject(position);
        imagen = json.getString(Imagen);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return imagen;
}

.

public static final String ProductoArray = "Nombre";
public static final String IDProducto = "idProducto";
public static final String Producto = "Nombre";
public static final String Precio = "Precio";
public static final String Descripcion = "Descripcion";
public static final String Disponibilidad = "Disponibilidad";
public static final String Imagen = "Imagen";
public static final String Cantidad_Entrada = "SUM(Cantidad_Entrada)";
public static final String Cantidad_Salida = "SUM(Cantidad_Salida)";
public static final String JSON_ARRAY = "result";
private JSONArray result;

private ArrayList<String> arrayList;
//private BitmapFactory bitmap;

TextView tv_id, tv_precio, tv_descripcion, tv_disponibilidad, tv_existencia, tv_e, tv_s;
ImageView imageView;

Spinner spinner;

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

    getSupportActionBar().hide();

    tv_e = findViewById(R.id.txt_entrada);
    tv_s = findViewById(R.id.txt_salida);
    tv_id = findViewById(R.id.txt_AID);
    tv_precio = findViewById(R.id.txtv_APrecio);
    tv_descripcion = findViewById(R.id.txtv_ADescripcion);
    tv_disponibilidad = findViewById(R.id.txtv_ADisponibilidad);
    tv_existencia = findViewById(R.id.txt_AExistencia);
    imageView = findViewById(R.id.AlbumImagen);
    spinner = findViewById(R.id.AlbumSpinner);

    arrayList = new ArrayList<String>();
    getdata();
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            tv_id.setText("ID: " + getId(position));
            tv_precio.setText("Precio: $"+getPrecio(position));
            tv_descripcion.setText("Descripcion: "+ getDescripcion(position));
            tv_e.setText(getEntrada(position));
            tv_s.setText(getSalida(position));

            int entrada = Integer.parseInt(tv_e.getText().toString());
            int salida = Integer.parseInt(tv_s.getText().toString());

            int existencia = entrada - salida;
            tv_existencia.setText("Existencia: " +existencia);

            if (getDisponibilidad(position).equals("1")){
                tv_disponibilidad.setText("Disponibilidad: SI");
            } else{
                tv_disponibilidad.setText("Disponibilidad: NO");
            }

           imageView.setImageBitmap(getStringImage(position));


        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            tv_id.setText("");
            tv_precio.setText("");
            tv_descripcion.setText("");
        }
    });
}



private void getdata() {
    StringRequest stringRequest = new StringRequest("http://10.1.7.94/Proyecto/consulta_producto1.php",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject j = null;
                    try {
                        j = new JSONObject(response);
                        result = j.getJSONArray(JSON_ARRAY);
                        empdetails(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}
private void empdetails(JSONArray j) {
    for (int i = 0; i < j.length(); i++) {
        try {
            JSONObject json = j.getJSONObject(i);
            arrayList.add(json.getString(ProductoArray));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    spinner.setAdapter(new ArrayAdapter<String>(Album.this, R.layout.spinner_item_colores, arrayList));
}

private String getNombre(int position){
    String name="";
    try {
        JSONObject json = result.getJSONObject(position);

        name = json.getString(Producto);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return name;
}

private String getId(int position){
    String id="";
    try {
        JSONObject json = result.getJSONObject(position);

        id = json.getString(IDProducto);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return id;
}

private String getPrecio(int position){
    String precio ="";
    try {
        JSONObject json = result.getJSONObject(position);

        precio = json.getString(Precio+"");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return precio;
}

private String getDescripcion(int position){
    String descripcion="";
    try {
        JSONObject json = result.getJSONObject(position);

        descripcion = json.getString(Descripcion);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return descripcion;
}

private String getDisponibilidad(int position){
    String disponibilidad="";
    try {
        JSONObject json = result.getJSONObject(position);

        disponibilidad = json.getString(Disponibilidad);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return disponibilidad;
}

public String getStringImage(int position){
    String imagen = "";
    byte[] encodeByte = Base64.decode(Imagen, Base64.DEFAULT);
    Bitmap bitmap= BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
    try{
        JSONObject json = result.getJSONObject(position);
        imagen = json.getString(Imagen);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return imagen;
}

private String getEntrada(int position){
    String entrada ="";
    try {
        JSONObject json = result.getJSONObject(position);

        entrada = json.getString(Cantidad_Entrada);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return entrada;
}

private String getSalida(int position){
    String salida ="";
    try {
        JSONObject json = result.getJSONObject(position);

        salida = json.getString(Cantidad_Salida);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return salida;
}

PHP:

<?php 
$sql = "SELECT producto.idProducto, Nombre, Precio, producto.Descripcion, Disponibilidad, Imagen, SUM(Cantidad_Entrada), SUM(Cantidad_Salida) FROM producto INNER JOIN album ON producto.idProducto=album.idProducto INNER JOIN regmovimientos ON producto.idProducto=regmovimientos.idProducto GROUP BY idProducto";
$con = mysqli_connect("localhost", "root", "", "usuarios");
$r = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($r)){
     array_push($result,array(
    'idProducto'=>$row['idProducto'],
    'Nombre'=>$row['Nombre'],
    'Precio'=>$row['Precio'],  
    'Descripcion'=>$row['Descripcion'],
    'Disponibilidad'=>$row['Disponibilidad'],
    'Imagen'=>$row['Imagen'],
    'SUM(Cantidad_Entrada)'=>$row['SUM(Cantidad_Entrada)'],
    'SUM(Cantidad_Salida)'=>$row['SUM(Cantidad_Salida)']
    ));
}
echo json_encode(array('result'=>$result));
mysqli_close($con);
?>
    
asked by Miguel Angel Lozano Vázquez 07.08.2018 в 06:41
source

0 answers