i'm a beginner in android i'm make an app to insert data in sqlite database and i i have a null pointer exception when add the record to database
this code get the pic from imageview and convert it to bitmap then to byte to save it any suggest to avoid this exception here is the code
imageView1.setDrawingCacheEnabled(true);
imageView1.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
imageView1.layout(0, 0, imageView1.getMeasuredWidth(), imageView1.getMeasuredHeight());
imageView1.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(imageView1.getDrawingCache());
imageView1.setDrawingCacheEnabled(false);
save_picture=loginDataBaseAdapter.getBytes(b);
loginDataBaseAdapter.insertEntry(userName, password, save_picture);
Toast.makeText(getApplicationContext(), "Record created ", Toast.LENGTH_LONG).show();`}
there is the method to convert bitmap to byte
// convert from bitmap to byte array
public byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, stream);
return stream.toByteArray();
}
and there is the insert method
public void insertEntry(String userName,String password , byte[] photo )
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("USERNAME", userName);
newValues.put("PASSWORD",password);
newValues.put("Photo", photo);
// Insert the row into your table
db.insert("LOGIN", null, newValues);
}
and there is the db creation
static final String DATABASE_CREATE = "create table "+"LOGIN"+
"( " +"ID"+" integer primary key autoincrement,"+ "USERNAME text,PASSWORD text, Photo blob not null); ";
Thanks
Aucun commentaire:
Enregistrer un commentaire