android 拍照上傳照片
廢話不多說,直接進入主題,想要在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; } }轉自:http://blog.csdn.net/yaoyeyzq/article/details/7254679
本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!