1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| public class Register extends AppCompatActivity {
DatabaseHelper db;
Button btnchosen,btnajout;
private EditText editpseudo,editnom,editphone,editdob,editville,editsexe,editgroupe,editdatedon;
private ImageView pic;
byte[] photo;
private Contact datamodel;
// private dataAdapter data;
Bitmap bp;
private String fpseudo,fnom,fphone,fdob,fville,fsexe,fgroupe,fdatedon;
@Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
db = new DatabaseHelper(this);
editpseudo=(EditText)findViewById(R.id.input_pseudo);
editnom=(EditText)findViewById(R.id.input_pseudo);
editphone=(EditText)findViewById(R.id.input_pseudo);
editdob=(EditText)findViewById(R.id.input_pseudo);
editville=(EditText)findViewById(R.id.input_pseudo);
editsexe=(EditText)findViewById(R.id.input_pseudo);
editgroupe=(EditText)findViewById(R.id.input_pseudo);
editdatedon=(EditText)findViewById(R.id.input_pseudo);
pic=(ImageView)findViewById(R.id.imageView4);
btnajout=(Button)findViewById(R.id.btn_enr);
btnchosen=(Button)findViewById(R.id.btn_chosen);
btnajout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addContact();
}
});
btnchosen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectImage();
}
});
}
public void selectImage(){
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 2);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case 2:
if(resultCode == RESULT_OK){
Uri choosenImage = data.getData();
if(choosenImage !=null){
bp=decodeUri(choosenImage, 400);
pic.setImageBitmap(bp);
}
}
}
}
protected Bitmap decodeUri(Uri selectedImage, int REQUIRED_SIZE) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);
// The new size we want to scale to
// final int REQUIRED_SIZE = size;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
}
catch (Exception e){
e.printStackTrace();
}
return null;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private byte[] profileImage(Bitmap b){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 0, bos);
return bos.toByteArray();
}
private void getValues(){
fpseudo = editpseudo.getText().toString().trim();
fnom=editnom.getText().toString().trim();
fphone=editphone.getText().toString().trim();
fdob=editdob.getText().toString().trim();
fville=editville.getText().toString().trim();
fsexe=editsexe.getText().toString().trim();
fgroupe=editgroupe.getText().toString().trim();
fdatedon=editdatedon.getText().toString().trim();
photo = profileImage(bp);
}
//Insert data to the database
private void addContact(){
getValues();
db.addContacts(new Contact(fpseudo,fnom,fphone,fdob,fville,fsexe,fgroupe,fdatedon,photo));
Toast.makeText(getApplicationContext(),"Saved successfully", Toast.LENGTH_LONG).show();
} |
Partager