標籤:儲存手寫 儲存繪圖
本文是自己學習所做筆記,歡迎轉載,但請註明出處:http://blog.csdn.net/jesson20121020
之前,忘了寫如何將手寫和繪圖儲存,現在補上。
首先看如何儲存繪圖,先看:
因為記事本的繪圖功能主要用到了畫布,而在構建畫布時,指定了Bitmap,也就是說在畫布上的所畫的東西都被儲存在了Bitmap中,因此,我們只要儲存該Bitmap,就可以將我們的所繪製的圖形以圖片的形式儲存,主要代碼如下:
/* * 儲存所繪圖形 * 返回繪圖檔案的儲存路徑 * */ public String saveBitmap(){ //獲得系統目前時間,並以該時間作為檔案名稱 SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss"); Date curDate = new Date(System.currentTimeMillis());//擷取目前時間 String str = formatter.format(curDate); 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, 100, out); out.flush(); out.close(); //儲存繪圖檔案路徑 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; }
這裡的mBitmap就是儲存畫布內容的,我們將其儲存在SD卡的指定目錄下,並以時間作為檔案名稱,最後返回儲存該繪圖檔案的路徑,用於將其儲存到資料庫中。
再看,如何儲存手寫:
和儲存繪圖一樣,這裡同樣是以圖片的形式儲存在SD卡的指定目錄下,但是這裡有個問題,手寫Activity用的是幀式布局,下層是自訂的EditText,上層同樣是自定的View,用於繪圖,而我們的手寫最終是儲存在自訂的EditText中的,當然可以用文本的形式儲存手寫內容,但這裡為了簡單起見,以及為了儲存手寫的顏色大小等屬性,故以圖片的形式儲存。
因為自訂的EditText說到底還是View,所以這裡就需要將View轉換成圖片,代碼如下:
//將view轉換成圖片et_handwrite.setDrawingCacheEnabled(true);Bitmap cutHandwriteBitmap = Bitmap.createBitmap(et_handwrite.getDrawingCache());et_handwrite.setDrawingCacheEnabled(false);
這樣,我們就將手寫的內容儲存在了Bitmap中,之後,我們就可以用儲存繪圖的方法將其儲存,如下:
......class ClickEvent implements OnClickListener{@Overridepublic void onClick(View v) {if(v == btn_save){//得到調用該Activity的Intent對象 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();}} }......//儲存手寫檔案 public String saveBitmap(){ //獲得系統目前時間,並以該時間作為檔案名稱 SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss"); Date curDate = new Date(System.currentTimeMillis());//擷取目前時間 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(); } } //將view轉換成圖片 et_handwrite.setDrawingCacheEnabled(true); Bitmap cutHandwriteBitmap = Bitmap.createBitmap(et_handwrite.getDrawingCache()); et_handwrite.setDrawingCacheEnabled(false);try {//儲存繪圖檔案路徑paintPath = "/sdcard/notes/" + str;FileOutputStream out = new FileOutputStream(file);cutHandwriteBitmap.compress(Bitmap.CompressFormat.PNG, 100, out); out.close(); } catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} return paintPath;}
至此,我們就實現了將記事本中添加的圖片及手寫檔案儲存在指定目錄,並以目前時間作為檔案名稱,這裡儲存檔案的函數返回的是圖片的路徑,這是為了將記事本中的內容儲存到資料庫中時,只將繪圖及手寫的儲存路徑儲存,而不是將整個繪圖或手寫儲存在資料庫中,這些內容,之前已有介紹,不再贅述。
android項目 之 記事本(15) ----- 儲存手寫及繪圖