I have an app with product registration that has 3 EditText and 2 imageView. I am trying to save these values in SQLite but there is something wrong because it does not give error but also does not save, it follows the code.
mBtnAdd.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mSQLiteHelper.insertData(
mEdtName.getText().toString().trim(),
mEdtAge. getText().toString().trim(),
mEdtPhone.getText().toString().trim(),
imageViewToByte( mImageView),
codeViewToByte( ivQRCode)
);
Toast.makeText( Produtos.this, "Salvo com sucesso", Toast.LENGTH_SHORT ).show();
mEdtName.setText( "" );
mEdtAge.setText( "" );
mEdtPhone.setText( "" );
mImageView.setImageResource( R.drawable.addphoto );
ivQRCode.setImageResource( R.mipmap.ic_launcher_foreground );
} catch (Exception e) {
e.printStackTrace();
}
}
} );
public static byte[] imageViewToByte(ImageView image) {
Bitmap bitmap1 = ((BitmapDrawable) image.getDrawable()).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap1.compress( Bitmap.CompressFormat.PNG, 100, stream );
byte[] byteArray = stream.toByteArray();
return byteArray;
}
public static byte[] codeViewToByte(ImageView code) {
Bitmap bitmap2 = ((BitmapDrawable) code.getDrawable()).getBitmap();
ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
bitmap2.compress( Bitmap.CompressFormat.PNG, 100, stream2 );
byte[] byteArray2 = stream2.toByteArray();
return byteArray2;
}
method insertData
public void insertData(String name, String age, String phone, byte[] image, byte[] code ){
SQLiteDatabase database = getWritableDatabase();
String sql = "INSERT INTO RECORD VALUES(NULL, ?, ?, ?, ? )";
SQLiteStatement statement = database.compileStatement( sql );
statement.clearBindings();
statement.bindString( 1,name );
statement.bindString( 2,age );
statement.bindString( 3,phone);
statement.bindBlob ( 4,image);
statement.bindBlob ( 5,code );
statement.executeInsert();
}