1 protected void startCamera(int code) { 2 bitmapCamera = null; 3 4 // 激活系统的照相机进行拍照 5 Intent intent = new Intent(); 6 intent.setAction("android.media.action.IMAGE_CAPTURE"); 7 intent.addCategory("android.intent.category.DEFAULT"); 8 9 startActivityForResult(intent,code); 10 }
1 public void onActivityResult(int requestCode, int resultCode, Intent data) { 2 // 拒绝时, 关闭页面, 缺少主要权限, 无法运行 3 if (requestCode == PERMISSIONS_CODE && resultCode == PermissionsActivity.PERMISSIONS_DENIED) { 4 Toast.makeText(getActivity().getApplicationContext(), "权限获取失败", Toast.LENGTH_SHORT).show(); 5 return; 6 } 7 if(requestCode == CAMERA_CARD_FRONT_CODE ||requestCode == CAMERA_USER_BCAK_CODE ||requestCode == CAMERA_USER_FRONT_CODE) { 8 Toast.makeText(getActivity(), "调用照相机完毕", Toast.LENGTH_SHORT).show(); 9 10 if (data.getData() != null || data.getExtras() != null) { //防止没有返回结果 11 Uri uri = data.getData(); 12 if (uri != null) { 13 bitmapCamera = BitmapFactory.decodeFile(uri.getPath()); //拿到图片 14 } 15 if (bitmapCamera == null) { 16 Bundle bundle = data.getExtras(); 17 if (bundle != null) { 18 bitmapCamera = (Bitmap) bundle.get("data"); 19 } else { 20 Toast.makeText(getActivity().getApplicationContext(), "找不到图片", Toast.LENGTH_SHORT).show(); 21 return; 22 } 23 } 24 } 25 switch (requestCode){ 26 case CAMERA_USER_FRONT_CODE: 27 userFrontImg.setImageBitmap(bitmapCamera); 28 break; 29 case CAMERA_USER_BCAK_CODE: 30 userBackImg.setImageBitmap(bitmapCamera); 31 break; 32 case CAMERA_CARD_FRONT_CODE: 33 cardFrontImg.setImageBitmap(bitmapCamera); 34 break; 35 } 36 } 37 }