簡體   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