Bonjour à tous,
Dans l'application que je développe, l'utilisateur clique sur un bouton qui ouvre une fenêtre dialogue qui lui permet de saisir sa signature.
Je souhaiterais enregistrer cette signature dans ma base de données.
Après quelques essaies non fluctuants, je viens vers vous.
Voici mon code :
EcatalogueActivity5.java :
CaptureSignature.java :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 public class EcatalogueActivity5 extends Activity implements View.OnClickListener { public Button btnEnregistrer, btnPrecedent, signature; public TextView dateCurrent; private int year, month, day; public RadioButton ancien_site, par_pigiste, par_client; public EditText url_site, nom_qualite, commentaires; public RadioGroup contenu_site; private DBManager dbM; public static final int SIGNATURE_ACTIVITY = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ecatalogue5); /*Bundle extras = getIntent().getExtras(); if (extras != null) { Intent intent = getIntent(); Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage"); }*/ btnEnregistrer = (Button) findViewById(R.id.btnEnregistrer); btnEnregistrer.setOnClickListener(this); btnPrecedent = (Button) findViewById(R.id.btnPrecedent); signature = (Button) findViewById(R.id.signature); ancien_site = (RadioButton) findViewById(R.id.ancien_site); par_pigiste = (RadioButton) findViewById(R.id.par_pigiste); par_client = (RadioButton) findViewById(R.id.par_client); contenu_site = (RadioGroup) findViewById(R.id.contenu_site); url_site = (EditText) findViewById(R.id.url_site); commentaires = (EditText) findViewById(R.id.commentaires); nom_qualite = (EditText) findViewById(R.id.nom_qualite); setCurrentDateOnView(); ancien_site.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (((RadioButton) v).isChecked()) { url_site.setFocusable(true); url_site.setFocusableInTouchMode(true); url_site.setCursorVisible(true); } else { url_site.setFocusable(false); url_site.setFocusableInTouchMode(false); url_site.setCursorVisible(false); } } }); par_pigiste.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (((RadioButton) v).isChecked()) { url_site.setFocusable(false); url_site.setFocusableInTouchMode(false); url_site.setCursorVisible(false); } } }); par_client.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (((RadioButton) v).isChecked()) { url_site.setFocusable(false); url_site.setFocusableInTouchMode(false); url_site.setCursorVisible(false); } } }); signature.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(EcatalogueActivity5.this, CaptureSignature.class); startActivity(intent); } }); // Bouton "Précédent", retour à l'activité précédente btnPrecedent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); } // Affichage de la date actuelle public void setCurrentDateOnView() { dateCurrent = (TextView) findViewById(R.id.dateCurrent); final Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); dateCurrent.setText(new StringBuilder().append(day).append("-").append(month + 1).append("-").append(year).append(" ")); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode) { case SIGNATURE_ACTIVITY: if (resultCode == RESULT_OK) { Bundle bundle = data.getExtras(); String status = bundle.getString("status"); if(status.equalsIgnoreCase("done")){ Toast toast = Toast.makeText(this, "Signature capture successful!", Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP, 105, 50); toast.show(); } } break; } } public void onClick(View v) { // Bouton "Suivant", passage à l'activité suivante... if (v == findViewById(R.id.btnEnregistrer)) { int len1 = nom_qualite.length(); if (!ancien_site.isChecked() && !par_pigiste.isChecked() && !par_client.isChecked()) { Toast.makeText(getApplicationContext(), "Veuillez saisir le champ \"Contenu\"", Toast.LENGTH_LONG).show(); } else if (len1 == 0) { Toast.makeText(getApplicationContext(), "Veuillez saisir le champ \"Nom et qualité\"", Toast.LENGTH_LONG).show(); } /*else if (!signature.isPressed()) { Toast.makeText(getApplicationContext(), "Veuillez saisir votre signature", Toast.LENGTH_LONG).show(); }*/ else { // On séléctionne le radiobutton séléctionné parmi tout le radiogroup et on le convertit en chaine de caractère contenu_site=(RadioGroup)findViewById(R.id.contenu_site); String rg1= ((RadioButton)this.findViewById(contenu_site.getCheckedRadioButtonId())).getText().toString(); // On convertit les edittext en chaine de caractére String et1 = url_site.getText().toString(); String et2 = commentaires.getText().toString(); String et3 = nom_qualite.getText().toString(); // On insére toute les valeurs précédentes dans la base de données ainsi que les radiobuttons (1 si il est coché sinon 0) INFOS_COMPLEMENTAIRES cus = new INFOS_COMPLEMENTAIRES(); cus.setUrlSite(et1); cus.setCommentaires(et2); cus.setNomQualite(et3); cus.setContenuSite(rg1); insertText(cus); // Si toutes les conditions sont remplies, passage à la seconde activité Intent intent = new Intent(EcatalogueActivity5.this, EcatalogueActivity5.class); startActivity(intent); } } } // Ajout des valeurs dans la base de données public void insertText(INFOS_COMPLEMENTAIRES insert){ DBAdapter db = new DBAdapter(this); DBManager dbm = new DBManager(this); SQLiteDatabase sqliteDatabase = db.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(INFOS_COMPLEMENTAIRES.CONTENU_SITE, insert.getContenu_site()); contentValues.put(INFOS_COMPLEMENTAIRES.URL_SITE, insert.getUrl_site()); contentValues.put(INFOS_COMPLEMENTAIRES.COMMENTAIRES, insert.getCommentaires()); contentValues.put(INFOS_COMPLEMENTAIRES.NOM_QUALITE, insert.getNom_qualite()); // contentValues.put(INFOS_COMPLEMENTAIRES.SIGNATURE, out.toByteArray()); long affectedColumnId = sqliteDatabase.insert(INFOS_COMPLEMENTAIRES.TABLE_INFOS_COMPLEMENTAIRES, null, contentValues); sqliteDatabase.close(); } }
DBAdapter.java :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296 public class CaptureSignature extends Activity { LinearLayout mContent; signature mSignature; Button mClear, mGetSign; public static String tempDir; public int count = 1; public String current = null; private Bitmap mBitmap; View mView; File mypath; private String uniqueId; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_signature); tempDir = Environment.getExternalStorageDirectory() + "/" + getResources().getString(R.string.external_dir) + "/"; ContextWrapper cw = new ContextWrapper(getApplicationContext()); File directory = cw.getDir(getResources().getString(R.string.external_dir), Context.MODE_PRIVATE); prepareDirectory(); uniqueId = getTodaysDate() + "_" + getCurrentTime() + "_" + Math.random(); current = uniqueId + ".png"; mypath = new File(directory,current); mContent = (LinearLayout) findViewById(R.id.linearLayout); mSignature = new signature(this, null); mSignature.setBackgroundColor(Color.WHITE); mContent.addView(mSignature, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); mClear = (Button)findViewById(R.id.clear); mGetSign = (Button)findViewById(R.id.getsign); mGetSign.setEnabled(false); mView = mContent; mClear.setOnClickListener(new OnClickListener() { public void onClick(View v) { Log.v("log_tag", "Panel Cleared"); mSignature.clear(); mGetSign.setEnabled(false); } }); mGetSign.setOnClickListener(new OnClickListener() { public void onClick(View v) { Log.v("log_tag", "Panel Saved"); boolean error = captureSignature(); if(!error){ mView.setDrawingCacheEnabled(true); mSignature.save(mView); Intent i = new Intent(CaptureSignature.this, EcatalogueActivity5.class); i.putExtra("BitmapImage", mBitmap); startActivity(i); Bundle b = new Bundle(); b.putString("status", "done"); Intent intent = new Intent(); intent.putExtras(b); setResult(RESULT_OK,intent); finish(); } } }); } @Override protected void onDestroy() { Log.w("GetSignature", "onDestory"); super.onDestroy(); } private boolean captureSignature() { boolean error = false; String errorMessage = ""; if(error){ Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP, 105, 50); toast.show(); } return error; } private String getTodaysDate() { final Calendar c = Calendar.getInstance(); int todaysDate = (c.get(Calendar.YEAR) * 10000) + ((c.get(Calendar.MONTH) + 1) * 100) + (c.get(Calendar.DAY_OF_MONTH)); Log.w("DATE:",String.valueOf(todaysDate)); return(String.valueOf(todaysDate)); } private String getCurrentTime() { final Calendar c = Calendar.getInstance(); int currentTime = (c.get(Calendar.HOUR_OF_DAY) * 10000) + (c.get(Calendar.MINUTE) * 100) + (c.get(Calendar.SECOND)); Log.w("TIME:",String.valueOf(currentTime)); return(String.valueOf(currentTime)); } private boolean prepareDirectory() { try { if (makedirs()) { return true; } else { return false; } } catch (Exception e) { e.printStackTrace(); return false; } } private boolean makedirs() { File tempdir = new File(tempDir); if (!tempdir.exists()) tempdir.mkdirs(); if (tempdir.isDirectory()) { File[] files = tempdir.listFiles(); for (File file : files) { if (!file.delete()) { System.out.println("Failed to delete " + file); } } } return (tempdir.isDirectory()); } public class signature extends View { private static final float STROKE_WIDTH = 5f; private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2; private Paint paint = new Paint(); private Path path = new Path(); private float lastTouchX; private float lastTouchY; private final RectF dirtyRect = new RectF(); public signature(Context context, AttributeSet attrs) { super(context, attrs); paint.setAntiAlias(true); paint.setColor(Color.BLACK); paint.setStyle(Paint.Style.STROKE); paint.setStrokeJoin(Paint.Join.ROUND); paint.setStrokeWidth(STROKE_WIDTH); } public void save(View v) { Log.v("log_tag", "Width: " + v.getWidth()); Log.v("log_tag", "Height: " + v.getHeight()); if(mBitmap == null) { mBitmap = Bitmap.createBitmap (mContent.getWidth(), mContent.getHeight(), Bitmap.Config.RGB_565);; } Canvas canvas = new Canvas(mBitmap); try { FileOutputStream mFileOutStream = new FileOutputStream(mypath); v.draw(canvas); mBitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream); mFileOutStream.flush(); mFileOutStream.close(); String url = Images.Media.insertImage(getContentResolver(), mBitmap, "title", null); Log.v("log_tag","url: " + url); //In case you want to delete the file //boolean deleted = mypath.delete(); //Log.v("log_tag","deleted: " + mypath.toString() + deleted); //If you want to convert the image to string use base64 converter } catch(Exception e) { Log.v("log_tag", e.toString()); } } public void clear() { path.reset(); invalidate(); } @Override protected void onDraw(Canvas canvas) { canvas.drawPath(path, paint); } @Override public boolean onTouchEvent(MotionEvent event) { float eventX = event.getX(); float eventY = event.getY(); mGetSign.setEnabled(true); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: path.moveTo(eventX, eventY); lastTouchX = eventX; lastTouchY = eventY; return true; case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_UP: resetDirtyRect(eventX, eventY); int historySize = event.getHistorySize(); for (int i = 0; i < historySize; i++) { float historicalX = event.getHistoricalX(i); float historicalY = event.getHistoricalY(i); expandDirtyRect(historicalX, historicalY); path.lineTo(historicalX, historicalY); } path.lineTo(eventX, eventY); break; default: debug("Ignored touch event: " + event.toString()); return false; } invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH), (int) (dirtyRect.top - HALF_STROKE_WIDTH), (int) (dirtyRect.right + HALF_STROKE_WIDTH), (int) (dirtyRect.bottom + HALF_STROKE_WIDTH)); lastTouchX = eventX; lastTouchY = eventY; return true; } private void debug(String string){ } private void expandDirtyRect(float historicalX, float historicalY) { if (historicalX < dirtyRect.left) { dirtyRect.left = historicalX; } else if (historicalX > dirtyRect.right) { dirtyRect.right = historicalX; } if (historicalY < dirtyRect.top) { dirtyRect.top = historicalY; } else if (historicalY > dirtyRect.bottom) { dirtyRect.bottom = historicalY; } } private void resetDirtyRect(float eventX, float eventY) { dirtyRect.left = Math.min(lastTouchX, eventX); dirtyRect.right = Math.max(lastTouchX, eventX); dirtyRect.top = Math.min(lastTouchY, eventY); dirtyRect.bottom = Math.max(lastTouchY, eventY); } } }
DBManager.java :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class DBAdapter extends SQLiteOpenHelper { // Initialisation de la base de données "cahierdescharges" public static final String DB_NAME = "cahierdescharges.db"; public static final int DB_VERSION = 25; public DBAdapter(Context context) { super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { // Création de la table "infos_complementaires" et de ses attributs String CREATE_TABLE_INFOS_COMPLEMENTAIRES = "create table if not exists " + INFOS_COMPLEMENTAIRES.TABLE_INFOS_COMPLEMENTAIRES + " ( " + PRODUITS.ID + " integer primary key autoincrement, " + INFOS_COMPLEMENTAIRES.MOTS_CLES + " text, " + INFOS_COMPLEMENTAIRES.LIEU_REFERENCEMENT + " text, " + INFOS_COMPLEMENTAIRES.PARTENAIRES + " text, " + INFOS_COMPLEMENTAIRES.FACEBOOK + " text, " + INFOS_COMPLEMENTAIRES.TWITTER + " text, " + INFOS_COMPLEMENTAIRES.GOOGLE_PLUS + " text, " + INFOS_COMPLEMENTAIRES.CONTENU_SITE + " text, " + INFOS_COMPLEMENTAIRES.URL_SITE + " text not null, " + INFOS_COMPLEMENTAIRES.COMMENTAIRES + " text not null, " + INFOS_COMPLEMENTAIRES.DATE + " DATETIME DEFAULT CURRENT_TIMESTAMP, " + INFOS_COMPLEMENTAIRES.NOM_QUALITE + " text not null, " + INFOS_COMPLEMENTAIRES.SIGNATURE + " BLOB not null);"; db.execSQL(CREATE_TABLE_INFOS_COMPLEMENTAIRES); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // Suppression de la table si elle existe déjà dans la base de données db.execSQL("DROP TABLE IF EXISTS " + INFOS_COMPLEMENTAIRES.TABLE_INFOS_COMPLEMENTAIRES); onCreate(db); } }
INFOS_COMPLEMENTAIRES.java :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class DBManager { private DBAdapter dba; private SQLiteDatabase db; private String[] resultColumns = new String[] { INFOS_COMPLEMENTAIRES.MOTS_CLES, INFOS_COMPLEMENTAIRES.LIEU_REFERENCEMENT, INFOS_COMPLEMENTAIRES.PARTENAIRES, INFOS_COMPLEMENTAIRES.FACEBOOK, INFOS_COMPLEMENTAIRES.TWITTER, INFOS_COMPLEMENTAIRES.GOOGLE_PLUS, INFOS_COMPLEMENTAIRES.CONTENU_SITE, INFOS_COMPLEMENTAIRES.URL_SITE, INFOS_COMPLEMENTAIRES.COMMENTAIRES, INFOS_COMPLEMENTAIRES.DATE, INFOS_COMPLEMENTAIRES.NOM_QUALITE, INFOS_COMPLEMENTAIRES.SIGNATURE }; public DBManager(Context appContext) { dba = new DBAdapter(appContext); } private SQLiteDatabase open() { return dba.getWritableDatabase(); } public void close() { if (dba != null) { dba.close(); } } public List<INFOS_COMPLEMENTAIRES> getInfosComplementairesFromCursor(Cursor cursor) { if (cursor != null) { List<INFOS_COMPLEMENTAIRES> list = new ArrayList<INFOS_COMPLEMENTAIRES>(); if (cursor.moveToFirst()) { do { INFOS_COMPLEMENTAIRES cus = new INFOS_COMPLEMENTAIRES(); cus.setMotsCles(cursor.getString(0)); cus.setLieuReferencement(cursor.getString(1)); cus.setPartenaires(cursor.getString(2)); cus.setFacebook(cursor.getString(3)); cus.setTwitter(cursor.getString(4)); cus.setGooglePlus(cursor.getString(5)); cus.setContenuSite(cursor.getString(6)); cus.setUrlSite(cursor.getString(7)); cus.setCommentaires(cursor.getString(8)); cus.setNomQualite(cursor.getString(9)); // cus.setSignature(cursor.getBlob(10)); list.add(cus); } while (cursor.moveToNext()); cursor.close(); } return list; } else { return null; } } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class INFOS_COMPLEMENTAIRES { // Initialisation de la table "infos_complementaires" et de ses attributs public static final String TABLE_INFOS_COMPLEMENTAIRES = "infos_complementaires"; public static final String MOTS_CLES = "mots_cles"; public static final String LIEU_REFERENCEMENT = "lieu_referencement"; public static final String PARTENAIRES = "partenaires"; public static final String FACEBOOK = "facebook"; public static final String TWITTER = "twitter"; public static final String GOOGLE_PLUS = "google_plus"; public static final String CONTENU_SITE = "contenu_site"; public static final String URL_SITE = "url_site"; public static final String COMMENTAIRES = "commentaires"; public static final String DATE = "date"; public static final String NOM_QUALITE = "nom_qualite"; public static final String SIGNATURE = "signature"; public static final String ID = "id"; private String mots_cles; private String lieu_referencement; private String partenaires; private String facebook; private String twitter; private String google_plus; private String contenu_site; private String url_site; private String commentaires; private String nom_qualite; private Bitmap signature; private int rowid; public String getMots_cles() { return mots_cles; } public void setMotsCles(String mots_cles) { this.mots_cles = mots_cles; } public String getLieu_referencement() { return lieu_referencement; } public void setLieuReferencement(String lieu_referencement) { this.lieu_referencement = lieu_referencement; } public String getPartenaires() { return partenaires; } public void setPartenaires(String partenaires) { this.partenaires = partenaires; } public String getFacebook() { return facebook; } public void setFacebook(String facebook) { this.facebook = facebook; } public String getTwitter() { return twitter; } public void setTwitter(String twitter) { this.twitter = twitter; } public String getGoogle_plus() { return google_plus; } public void setGooglePlus(String google_plus) { this.google_plus = google_plus; } public String getContenu_site() { return contenu_site; } public void setContenuSite(String contenu_site) { this.contenu_site = contenu_site; } public String getUrl_site() { return url_site; } public void setUrlSite(String url_site) { this.url_site = url_site; } public String getCommentaires() { return commentaires; } public void setCommentaires(String commentaires) { this.commentaires = commentaires; } public String getNom_qualite() { return nom_qualite; } public void setNomQualite(String nom_qualite) { this.nom_qualite = nom_qualite; } public Bitmap getSignature() { return signature; } public void setSignature(Bitmap signature) { this.signature = signature; } public int getRowid() { return rowid; } public void setRowid(int rowid) { this.rowid = rowid; } }
Je vous remercie d'avance pour votre aide apportée.
Partager