繁体   English   中英

如何在 android 工作室中从 SQLite 中检索图像

[英]How retrieve image from SQLite in android studio

我可以保存 4 个值,但我只检索字符串

public class CafeteriaDB extends SQLiteOpenHelper {

    private static final String NOMBRE_DB = "cafeteria.db";
    private static final int VERSION_DB = 1;
    private static final String TABLA_BEBIDAS = "CREATE TABLE BEBIDAS(NOMBRE TEXT, PRECIO TEXT, INFO TEXT, FOTO BLOB)";

    public CafeteriaDB(@Nullable Context context) {
        super(context, NOMBRE_DB, null, VERSION_DB);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABLA_BEBIDAS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+ TABLA_BEBIDAS);
        db.execSQL(TABLA_BEBIDAS);
    }

    public void agregarProducto(String nombre, String precio, String info, byte[] foto){
        SQLiteDatabase db=getWritableDatabase();
        if(db!=null){
            if(info.equals("Bebidas")){
                db.execSQL("INSERT INTO BEBIDAS VALUES('"+nombre+"','"+precio+"','"+info+"','"+foto+"')");
                db.close();
            }
        }
    }

    public ArrayList<CarritoVo> mostrarBebidas(){
        SQLiteDatabase db=getReadableDatabase();
        Cursor cursor= db.rawQuery("SELECT * FROM BEBIDAS",null);
        ArrayList<CarritoVo> bebidas=new ArrayList<>();
        if(cursor.moveToFirst()){
            do{
                bebidas.add(new CarritoVo(cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getBlob(3)));
            }while(cursor.moveToNext());
        }
        return bebidas;
    }

    public void eliminarProducto(String nombre, String info){
        SQLiteDatabase db=getWritableDatabase();
        if(db != null){
            if(info.equals("Bebidas")){
                db.execSQL("DELETE FROM BEBIDAS WHERE NOMBRE='"+nombre+"'");
                db.close();
            }
        }
    }
}


      Button btnCarrito = (Button)myDialog.findViewById(R.id.btnCarrito);
               btnCarrito.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       try {
                           cafeteriaDB.agregarProducto(
                                   dialogName.getText().toString().trim(),
                                   dialogCantidad.getText().toString().trim(),
                                   dialogInfo.getText().toString().trim(),
                                   imageViewToByte(dialog_foto));
                           Toast.makeText(parent.getContext(),"Producto Añadido Correctamente",Toast.LENGTH_SHORT).show();
                       }
                       catch (Exception e){
                           e.printStackTrace();
                       }
                   }
               });

               myDialog.show();
            }
        });

        return myHolder;

    }

    private static byte[] imageViewToByte(ImageView dialog_foto) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Bitmap bitmap = ((BitmapDrawable)dialog_foto.getDrawable()).getBitmap();
        bitmap.compress(Bitmap.CompressFormat.PNG,100, stream);
        byte[] byteArray = stream.toByteArray();
        return byteArray;
    }


emphasized text
      @Override
    public void onBindViewHolder(@NonNull ViewHolderCarrito holder, int position) {
       byte[] foto =listaCarrito.get(position).getFoto();
       ByteArrayInputStream fotoStream = new ByteArrayInputStream(foto);
       Bitmap thefoto = BitmapFactory.decodeByteArray(foto,0,foto.length);
       holder.etiFoto.setImageBitmap(thefoto);
        holder.etiNombre.setText(listaCarrito.get(position).getNombre());
        holder.etiPrecio.setText(listaCarrito.get(position).getPrecio());
        holder.etiInfo.setText(listaCarrito.get(position).getInfo());
    }
}

插入方法:-

public void agregarProducto(String nombre, String precio, String info, byte[] foto){
    SQLiteDatabase db=getWritableDatabase();
    if(db!=null){
        if(info.equals("Bebidas")){
            db.execSQL("INSERT INTO BEBIDAS VALUES('"+nombre+"','"+precio+"','"+info+"','"+foto+"')");
            db.close();
        }
    }
}

不会插入 BLOB,它可能会插入 byte[] 指针的值。

必须将 BLOB 指定为x'??????????' 其中问号是数组中字节的十六进制表示。 例如 x'00F1F2F3'。

将字节数组转换为合适格式的简单方法是让 SQLiteDatabase 便捷方法insert代表您进行转换。

因此,将上述方法更改为:-

public long agregarProducto(String nombre, String precio, String info, byte[] foto){
    SQLiteDatabase db=getWritableDatabase();
    ContentValues cv = new ContentValues();

    if(db!=null){
        if(info.equals("Bebidas")){
            cv.put("NOMBRE",nombre);
            cv.put("PRECIO",precio);
            cv.put("INFO",info);
            cv.put("FOTO",foto);
            db.insert("BEBIDAS",null,cv);
            db.close();
        }
    }
}
  • 请注意,该方法将返回一个long,如果插入有效,它将> 0,否则它将是-1,表示没有插入任何内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM