SQLite
 sql >> Base de Dados >  >> RDS >> SQLite

Como recuperar uma imagem de um banco de dados SQLite?


Para inserir imagem no banco de dados:
    Bitmap bitmap = ((BitmapDrawable) image_imgv.getDrawable()).getBitmap();

    ByteArrayOutputStream bos4 = new ByteArrayOutputStream();
    bitmap4.compress(Bitmap.CompressFormat.PNG, 100, bos4);
    image = bos4.toByteArray();

    database = new BBDD(this, "BBDD", null, 1);
    SQLiteDatabase db = database.getWritableDatabase();

    ContentValues reg = new ContentValues();
    reg.put("img", image);

Para recuperar:
database2 = new BBDD(Activity.this, "BBDD", null, 1);
            SQLiteDatabase db2 = database2.getReadableDatabase();

            if (db2 != null)
            {
                Cursor cursor = db2.rawQuery("SELECT img FROM database2, null);
                if (cursor.moveToFirst())
                {
                    img=cursor.getBlob(cursor.getColumnIndex("img"));
                    Bitmap b1=BitmapFactory.decodeByteArray(image, 0, image.length);
                    image_imageview.setImageBitmap(b1);

                }
                else
                    Toast.makeText(Activity.this, "Error.", Toast.LENGTH_LONG).show();

                db2.close();
            }
            else
                Toast.makeText(sActivity.this, "Error db.", Toast.LENGTH_LONG).show();
        }
    });