0
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 there is no error but also not saved, follow 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();
}
Just one note: The good practice is to save the files in a specific folder and store only their path in the database. Research better on this subject if you want to know more. But of course, all part of the need too.
– rbz
It is wrong to write byte of images in the database?
– rbz
Agree with @RBZ, you treat byte images is very boring, you uploading the image and saving the way is much more practical
– gabrielfalieri
Thank you. But the images I’m using are generated images, the first with the camera phone and the other a Qrcode image,
– user116985
@Danielraibolt here what he does with the images is you define, if it goes to the bank, if it is stored on the device, in internal or external memory, etc. But, it was only a hint to know that it is not used in this way.
– rbz
I reversed your edition, because you are storing image in the bank. The new title that I had placed, it was strange.
– rbz
Guys, I would like if someone could help me see in my project what might be causing the error, I’m a beginner and I’m struggling to find the problem.
– user116985