有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在安卓 studio中从SQLite检索图像

我可以保存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());
    }
}

共 (1) 个答案

  1. # 1 楼答案

    插入方法:

    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,它可能会插入字节[]指针的值

    BLOB必须指定为x'??????????',其中问号是数组中字节的十六进制表示形式。e、 g.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,表示未插入任何内容