0

Buenas, estoy realizando una aplicación muy sencilla que guarda imágenes en SQLite para luego mostrarlas en un ListItem con celdas personalizadas.

Cada imagen tiene un nombre una descripción y una ID, los cuales guardo también en SQLite. El problema es que la imagen no se muestra pero el nombre, descripción e ID que escribes si.

He probado de todo y ya no se me ocurre nada más. Os dejo el código a ver si me podéis echar una mano..

public void cargarLista()
{
    db = usdbh.getReadableDatabase();
    if (db != null)
    {
        Cursor c = db.rawQuery("SELECT * FROM Notas ORDER BY codigo ASC", null);
        //Nos aseguramos de que existe al menos un registro
        if (c != null && c.moveToFirst()) {
            //Recorremos el cursor hasta que no haya más registros
            int cuenta = c.getCount();
            datosLista = new Nota[cuenta];
            int i = 0;
            do {
                datosLista[i] = new Nota();
                datosLista[i].setCodigo(c.getInt(0));
                datosLista[i].setNombre(c.getString(1));
                datosLista[i].setContenido(c.getString(2));
                datosLista[i].setImagen(c.getBlob(3)); // -> Esto devuelve algo parecido a [SH21D398D  es solo un ejemplo
                i++;
            } while (c.moveToNext());
            AdaptadorUsuarios adaptador = new AdaptadorUsuarios(this);
            lstLista.setAdapter(adaptador);
            adaptador.notifyDataSetChanged();
        }
        else
        {
            // Haremos desaparecer el último item 'borrando' el adaptador
            lstLista.setAdapter(null);
        }
    }
}
class AdaptadorUsuarios extends ArrayAdapter
{
    Activity context;
    AdaptadorUsuarios(Activity context)
    {
        super(context, R.layout.listitem_nota, datosLista);
        this.context = context;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        View item = convertView;
        ViewHolder holder;
        if (item == null)
        {
            LayoutInflater inflater = context.getLayoutInflater();
            item = inflater.inflate(R.layout.listitem_nota, null);

            holder = new ViewHolder();
            holder.codigo = (TextView)item.findViewById(R.id.LblCodigo);
            holder.nombre = (TextView)item.findViewById(R.id.LblNombre);
            holder.email = (TextView)item.findViewById(R.id.LblEmail);
            holder.imagen = (ImageView)item.findViewById(R.id.LblImagen);
            item.setTag(holder);
        }else{
            holder = (ViewHolder)item.getTag();
        }
        // Añadiremos el contenido de la nota a nuestro listitem personalizado.

        // Esta línea está activa para mostrar la ID en SQL mientras se desarolla la APP.
        holder.codigo.setText(datosLista[position].getCodigo() + "");

        holder.nombre.setText(cortar(datosLista[position].getNombre(), 37));
        holder.email.setText(cortar(datosLista[position].getContenido(), 55));

        // Obtenemos el BLOB de la imagen y la creamos en bitmap
        byte[] icono = datosLista[position].getImagen();
        ByteArrayInputStream imageStream = new ByteArrayInputStream(icono);
        Bitmap theImage = BitmapFactory.decodeStream(imageStream);
        holder.imagen.setImageBitmap(theImage);
        return(item);
    }

    public String cortar(String str, int len)
    {
        if (str.length() > len) {
            return str.substring(0, len) + "...";
        } else {
            return str;
        }
    }
}

static class ViewHolder {
    TextView codigo;
    TextView nombre;
    TextView email;
    ImageView imagen;
}

1 respuesta 1

1

Intenta convertir tu imagen al formato Bitmap

final byte[] photo = 

UserConfig.getInstance().getCurrentUser().getPhoto();
   if (photo != null) {
      final Bitmap decodedByte = BitmapFactory.decodeByteArray(photo, 0, photo.length);
      img.setImageBitmap(decodedByte);
    }

https://stackoverflow.com/questions/2415619/how-to-convert-a-bitmap-to-drawable-in-android

2
  • Hola, ya lo hice y sigue igual.. No consigo mostrar la imagen.. el 25 abr. 2017 a las 20:05
  • tal vez ya sea un poco tardado la respuesta, pero a otros puede servirle para que el método que comente arriba funcione es necesario que tu imagen sea de tipo byte array
    – Devix
    el 8 oct. 2018 a las 14:35

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.