This article is your own study notes, welcome reprint, but please indicate the source: http://blog.csdn.net/jesson20121020
Before, forgot to write how to save handwriting and drawings, now fill it up.
First look at how to save the drawing, first look at:
Because the drawing function of Notepad is mainly used in the canvas, and when the canvas is built, bitmap is specified, that is, the painting on the canvas is saved in the bitmap, so we just save the bitmap, we can save our drawing as a picture, the main code is as follows:
/* Save the drawing * Returns the path to the plot file * */Public String Savebitmap () {//Gets the current time of the system and takes that time as the file name Si Mpledateformat formatter = new SimpleDateFormat ("Yyyymmddhhmmss"); Date curdate = new Date (System.currenttimemillis ());//Get current time String str = Formatter.format (Curda TE); String Paintpath = ""; str = str + "Paint.png"; File dir = new file ("/sdcard/notes/"); File File = new file ("/sdcard/notes/", str); if (!dir.exists ()) {Dir.mkdir (); } else{if (File.exists ()) {file.delete (); }} try {FileOutputStream out = new FileOutputStream (file); Mbitmap.compress (Bitmap.CompressFormat.PNG, N, out); Out.flush (); Out.close (); Save drawing file path Paintpath = "/sdcard/notes/" + str; } catch (FileNotFoundException e) {//TODO auto-generated catch block E.PRintstacktrace (); } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); } return Paintpath; }
the Mbitmap here is to save the canvas content, we store it in the specified directory of the SD card, take the time as the file name, and finally return the path where the drawing file is stored for storing it in the database.
Look again, how to save handwriting:
As well as saving the drawing, this is also saved as a picture in the SD card in the specified directory, but there is a problem, handwriting activity with a frame layout, the lower layer is a custom edittext, the upper layer is also a custom view, for drawing, And our handwriting is finally saved in the custom EditText, of course, you can save the handwriting in the form of text, but for the sake of simplicity, and in order to save the handwriting color size and other properties, it is saved as a picture.
Since the custom edittext is still a view, it is necessary to convert the view into a picture, the code is as follows:
Convert view to Picture et_handwrite.setdrawingcacheenabled (true); Bitmap Cuthandwritebitmap = Bitmap.createbitmap (Et_handwrite.getdrawingcache ()); et_ Handwrite.setdrawingcacheenabled (FALSE);
In this way, we save the handwritten content in the bitmap, and then we can save the drawing by saving it as follows:
... class clickevent implements onclicklistener{@Overridepublic void OnClick (View v) {if (v = = Btn_save) {//get called the Activi Ty's Intent object Intent Intent = Getintent (); Bundle B = new bundle (); String path = Savebitmap (); B.putstring ("Handwritepath", Path); Intent.putextras (b); Setresult (RESULT_OK, intent); HandWriteActivity.this.finish ();} else if (v = = btn_back) {HandWriteActivity.this.finish ();}} }......//Save the handwritten file public String Savebitmap () {//Get the current time of the system and take that time as the file name SimpleDateFormat formatter = new Simpledatefor Mat ("Yyyymmddhhmmss"); Date curdate = new Date (System.currenttimemillis ());//Gets the current time String str = Formatter.format (curdate); String Paintpath = ""; str = str + "Write.png"; File dir = new file ("/sdcard/notes/"); File File = new file ("/sdcard/notes/", str); if (!dir.exists ()) {Dir.mkdir (); } else{if (File.exists ()) {file.delete (); }}//Convert view to diagramSlice et_handwrite.setdrawingcacheenabled (true); Bitmap Cuthandwritebitmap = Bitmap.createbitmap (Et_handwrite.getdrawingcache ()); Et_handwrite.setdrawingcacheenabled (false); try {//Save drawing file path Paintpath = "/sdcard/notes/" + str; FileOutputStream out = new FileOutputStream (file); cuthandwritebitmap.compress (Bitmap.CompressFormat.PNG, N, out); Out.close (); } catch (FileNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO Auto-generated catch Blocke.printstacktrace ();} return Paintpath;}
At this point, we realized that the pictures and handwritten files added in Notepad are saved in the specified directory, and the current time as the file name, where the function of saving the file returns the path of the picture, in order to store the contents of Notepad in the database, only the drawing and handwritten storage path is saved, Instead of saving the entire drawing or handwriting in the database, these things have been introduced before and are not covered.
Notepad for Android Project-----Save handwriting and drawings