Bitmapfactory.decodeByteArray Returning null from Sqlite

Asked

Viewed 15 times

1

I am creating an application that I register a search image from the mobile gallery, saving it in SQLITE, and searching later to show in a view Recycler, however, debugging realized that the bitmap object is getting null.

Obs1: the information is being saved as SQLITE BLOB object,

Obs2: I’m manipulating the image as byte[]

follows some code

INSERTION IN SQLITE:

pacote.img = imageViewToByte(imag_pacote);


public static byte[] imageViewToByte(ImageView image) {
    Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}


pacoteRepositorio.inserir(pacote);

  public void inserir(Pacote pacote){
    ContentValues contentValues = new ContentValues();
    contentValues.put("nome", pacote.nome);
    contentValues.put("img", String.valueOf(pacote.img));

    conexao.insertOrThrow("PACOTE",null, contentValues);
}

Theoretically it’s working properly !!!!

SEARCH CODE *HERE I BELIEVE IS THE ERROR

Public list of searchPacksPendentes() throws Parseexception { List packages = new Arraylist();

    StringBuilder sql = new StringBuilder();
    sql.append("  SELECT * ");
    sql.append("  FROM PACOTE");
    sql.append("  WHERE pendente = 1");

    Cursor resultado = conexao.rawQuery(sql.toString(), null);

    if(resultado.getCount() > 0){
        resultado.moveToFirst();

        do{
            SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
            Pacote pac  = new Pacote();
            pac.id       = resultado.getInt(resultado.getColumnIndexOrThrow("id"));
            pac.nome       = resultado.getString(resultado.getColumnIndexOrThrow("nome"));
            pac.img    = resultado.getBlob(resultado.getColumnIndexOrThrow("img"));

            pacotes.add(pac);

        }while(resultado.moveToNext());
    }
    return pacotes;
}

 byte[] imagen = pacote.img;
        Bitmap bitmap = BitmapFactory.decodeByteArray(imagen,0, imagen.length); //<< RETORNANDO NULL
        holder.img_pacote.setImageBitmap(bitmap);

Here it shows that BYTE[] is arriving normally, however when I put in Bitmap GETS null

obs3: I saw that the object (byte[]imagen) is receiving an array normally, with 11 positions

1 answer

0

RESOLVED !!

The problem was time to play byte[] in the database, it had about 215,000 positions but when I tried to insert it was only 11 because parsiei for STRING

contentValues.put("img", String.valueOf(pacote.img));

the correct is

contentValues.put("img", pacote.img);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.