Android 调用系统相机,拍照并返回显示

1.添加系统权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />

2.点击拍照,判断是否添加存储,拍照权限


private static final int CODE_CAMERA_REQUEST = 0xa1;
private void shenqingPermission() {
        if (ContextCompat.checkSelfPermission
                (this,
                        Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission
                        (this,
                              Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {

            //申请相机权限
            shenqing2();
        } else {
            //打开相册
            Toast.makeText(this, "有权限,直接打开相机", Toast.LENGTH_SHORT).show();
            //拍照
            takePicture(this,  CODE_CAMERA_REQUEST,"ddd");
        }
    }
public static final int daihao2 = 0x02;
private void shenqing2() {
//申请权限的方法
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA}, daihao2);
    }

申请权限的回调onRequestPermissionsResult

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case daihao2:
                if (grantResults.length > 0 && grantResults[1] == PackageManager.PERMISSION_GRANTED
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED
                        ) {
                    //拍照
                    takePicture(this, CODE_CAMERA_REQUEST,null);
                } else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "您拒绝了拍照权限,所以不能拍照", Toast.LENGTH_SHORT).show();

                } else if (grantResults.length > 0 && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "您拒绝了存储权限,所以不能拍照", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(this, "您拒绝了存储,拍照权限,所以不能拍照", Toast.LENGTH_SHORT).show();

                }
                break;
        }
    }

调用系统相机拍照拍照

     // 原图地址
    private String imgPath;
    // 临时保存文件名
    String fileName = null;
    /**
     *
     * 读取权限 授权之后 调用此方法
     * @param activity    当前activity
     * @param requestCode 调用系统相机请求码
     */
    public void takePicture(@NonNull Activity activity,@NonNull  int requestCode ,@Nullable String imgFileName) {

        String baseUrl = Environment.getExternalStorageDirectory().getAbsolutePath()+"/my_img";
        fileName = "";
        if (TextUtils.isEmpty(imgFileName))
            fileName = System.currentTimeMillis()+".jpg";
        else
            fileName = imgFileName+".jpg";
        // 保存拍照的路径
        Uri mImageCaptureUri;
        // 跳转系统相机的intent
        Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // 根据不同的Android版本uri不一样
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //android7.0以上
            ContentValues contentValues = new ContentValues(1);
           String absolutePath = new File(baseUrl, fileName).getAbsolutePath();
            contentValues.put(MediaStore.Images.Media.DATA, absolutePath);
            mImageCaptureUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        } else {
            File file = new File(baseUrl, fileName);
            mImageCaptureUri = Uri.fromFile(file);
        }
        takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
        startActivityForResult(takeIntent, requestCode);
    }

拍照的回调onActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

//拍照成功 resultCode == RESULT_OK
        if (resultCode == RESULT_OK && requestCode ==CODE_CAMERA_REQUEST) {
            imgPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/my_img/"+fileName;
            showImages(BitmapFactory.decodeFile(imgPath));
        }
    }
//显示图片
private void showImages(Bitmap bitmap) {
        img.setImageBitmap(bitmap);
    }

你可能感兴趣的:(android)