方法一:java
此方法會由Camera直接產生照片回傳給應用程序,可是返回的是壓縮圖片,顯示不清晰android
/** 啓動Camera */ private void intentCamera(){ try { Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, 0); } catch (ActivityNotFoundException e) { e.printStackTrace(); } }
/** 在onActivityResult中獲取圖片 */ private void getImgFromCamera(){ Bundle bundle = data.getExtras(); bm = (Bitmap) bundle.get("data"); if (bm != null) bm.recycle(); bm = (Bitmap) data.getExtras().get("data"); if(bm != null){ img.setImageBitmap(bm); } }
方法二:ide
此方法所拍即所得,可是會在Sd卡上產生臨時文件this
/*** 打開照相機 */ private void intentCamera(){ Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); File file = new File(Environment.getExternalStorageDirectory() + "/Images"); if(!file.exists()){ file.mkdirs(); } Uri mUri = Uri.fromFile( new File(Environment.getExternalStorageDirectory() + "/Images/", "cameraImg" + String.valueOf(System.currentTimeMillis()) + ".jpg")); cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri); cameraIntent.putExtra("return-data", true); startActivityForResult(cameraIntent, 1); }
/*** 獲取相機返回的數據 */ private void getImgFromCamera(){ ContentResolver cr = this.getContentResolver(); try { if(cameraBitmap != null) cameraBitmap.recycle();// 若是不釋放的話,不斷取圖片,將會內存不夠 cameraBitmap = BitmapFactory.decodeStream(cr.openInputStream(mUri)); if(bm != null){ img.setImageBitmap(bm); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block Log.e("error", "從相機中獲取圖片失敗====="); e.printStackTrace(); } }