廢話不多說,直接進入主題,想要在android中實現拍照最簡單餓方法就是New 一個 Intent 設定Action為android.media.action.IMAGE_CAPTURE 然後使用startActivityForResult(intent,REQUEST_CODE)方法進入相機。當然還有很多方式可以實現,大家可以在網上尋找。但是要注意的是在進入相機前最好判斷下sdcard是否可用,代碼如下:
destoryBimap();String state = Environment.getExternalStorageState();if (state.equals(Environment.MEDIA_MOUNTED)) {intent = new Intent("android.media.action.IMAGE_CAPTURE");startActivityForResult(intent, REQUEST_CODE);} else {Toast.makeText(DefectManagerActivity.this,R.string.common_msg_nosdcard, Toast.LENGTH_LONG).show();}
當拍照完成以後需要在onActivityResult(int requestCode, int resultCode, Intent data)方法中擷取拍攝的圖片,android把拍攝的圖片封裝到bundle中傳遞迴來,但是根據不同的機器獲得相片的方式不太一樣,所以會出現某一種方式擷取圖片為null的想象,解決辦法就是做一個判斷,當一種方式不能擷取,就是用另一種方式,下面是分別擷取相片的兩種方式:
Uri uri = data.getData();if (uri != null) {photo = BitmapFactory.decodeFile(uri.getPath());}if (photo == null) {Bundle bundle = data.getExtras();if (bundle != null) {photo = (Bitmap) bundle.get("data");} else {Toast.makeText(DefectManagerActivity.this,getString(R.string.common_msg_get_photo_failure),Toast.LENGTH_LONG).show();return;}}
第一種方式是用方法中傳回來的intent調用getData();方法擷取資料的Uri,然後再根據uri擷取資料的路徑,然後根據路徑封裝成一個bitmap就行了.
第二種方式也是用法中傳回來的intent對象但是不再是調用getData();方法而是調用getExtras();方法擷取intent裡面所有參數的一個對象集合bundle,然後是用bundle對象得到鍵為data的值也就是一個bitmap對象.
通過上面兩種方式就能擷取相片的bitmap對象,然後就可以在程式中是用,如果你想把相片儲存到自己指定的目錄可以是用如下步驟即可:
首先bitmap有個一compress(Bitmap.CompressFormat.JPEG, 100, baos)方法,這個方法有三個參數,第一個是指定將要儲存的圖片的格式,第二個是圖片儲存的品質,值是0-100,比如像PNG格式的圖片這個參數你可以隨便設定,因為PNG是無損的格式。第三個參數是你一個緩衝輸出資料流ByteArrayOutputStream();,這個方法的作用就是把bitmap的圖片轉換成jpge的格式放入輸出資料流中,然後大家應該明白怎麼操作了吧,下面是執行個體代碼:
String pictureDir = "";FileOutputStream fos = null;BufferedOutputStream bos = null;ByteArrayOutputStream baos = null;try {baos = new ByteArrayOutputStream();bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);byte[] byteArray = baos.toByteArray();String saveDir = Environment.getExternalStorageDirectory()+ "/temple";File dir = new File(saveDir);if (!dir.exists()) {dir.mkdir();}File file = new File(saveDir, "temp.jpg");file.delete();if (!file.exists()) {file.createNewFile();}fos = new FileOutputStream(file);bos = new BufferedOutputStream(fos);bos.write(byteArray);pictureDir = file.getPath();} catch (Exception e) {e.printStackTrace();} finally {if (baos != null) {try {baos.close();} catch (Exception e) {e.printStackTrace();}}if (bos != null) {try {bos.close();} catch (Exception e) {e.printStackTrace();}}if (fos != null) {try {fos.close();} catch (Exception e) {e.printStackTrace();}}}
然後就是實現圖片的上傳功能,我這裡是是用的apache的HttpClient裡面的MultipartEntity實現檔案上傳具體代碼如下:
/** * 提交參數裡有檔案的資料 * * @param url * 伺服器位址 * @param param * 參數 * @return 伺服器返回結果 * @throws Exception */public static String uploadSubmit(String url, Map<String, String> param,File file) throws Exception {HttpPost post = new HttpPost(url);MultipartEntity entity = new MultipartEntity();if (param != null && !param.isEmpty()) {for (Map.Entry<String, String> entry : param.entrySet()) {entity.addPart(entry.getKey(), new StringBody(entry.getValue()));}}// 添加檔案參數if (file != null && file.exists()) {entity.addPart("file", new FileBody(file));}post.setEntity(entity);HttpResponse response = httpClient.execute(post);int stateCode = response.getStatusLine().getStatusCode();StringBuffer sb = new StringBuffer();if (stateCode == HttpStatus.SC_OK) {HttpEntity result = response.getEntity();if (result != null) {InputStream is = result.getContent();BufferedReader br = new BufferedReader(new InputStreamReader(is));String tempLine;while ((tempLine = br.readLine()) != null) {sb.append(tempLine);}}}post.abort();return sb.toString();}
這裡就基本上對圖片上傳就差不多了,但是還有一個問題就是圖片上傳完以後bitmap還在記憶體中,而且大家都知道如果,高清的圖片比較大,而手機記憶體本來就有限,如果不進行處理很容易報記憶體溢出,所以我們應該把處理完的bitmap從記憶體中釋放掉,這時候就需要調用bitmap的recycle();方法,調用這個方法的時候需要注意不能太早也不能太晚,不然會報異常,一般可以放在下一張圖片產生前或者沒有任何view引用要銷毀的圖片的時候下面是執行個體代碼:
/** * 銷毀圖片檔案 */private void destoryBimap() {if (photo != null && !photo.isRecycled()) {photo.recycle();photo = null;}}
好了,這裡就講完了,如果大家還有什麼更好的方法,大家可以多交流交流。