相机权限的申请与系统相机的调用的实践

public void showDatePicker(View v) {
        TimePickerDialog timePickerDialog = new TimePickerDialog(this, new 			         TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

            }
        }, 12, 12, true);
        timePickerDialog.show();
    }

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }

    /**
     * api>23
     * 请求权限
     *
     * @param v
     */
    public void getCameraPermission(View v) {
        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            Toast.makeText(this, "没有相机", Toast.LENGTH_LONG).show();
            return;
        }
        if (PackageManager.PERMISSION_DENIED == getPackageManager().checkPermission(CAMERA, this.getPackageName())) {//23
            // Should we show an explanation?
            if (this.shouldShowRequestPermissionRationale(CAMERA)) {
                Log.i(TAG, "getCameraPermission:shouldShowRequestPermissionRationale " + "true");
                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {
                Log.i(TAG, "getCameraPermission:requestPermissions");
                this.requestPermissions(new String[]{CAMERA}, REQUEST_IMAGE_CAPTURE);
            }
        } else {
            Log.i(TAG, "getCameraPermission: " + "直接拍照");
            takePhoto(REQUEST_IMAGE_CAPTURE_SAVE);
        }
    }

    /**
     * 调用自身拍照
     *
     * @param mode REQUEST_IMAGE_CAPTURE_SAVE  照片保存
     *             REQUEST_IMAGE_CAPTURE 照片拍摄后预览 (缩略图)
     */
    public void takePhoto(int mode) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {//确保有activity去处理intent 防止crash
            if (mode == REQUEST_IMAGE_CAPTURE_SAVE) {
                File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
                    //错误处理 抓取日志 用来进行上传服务器进行修复
                    ex.printStackTrace();
                }
                if (photoFile != null) {
                    Uri photoURI = FileProvider.getUriForFile(this,
                            "com.sudy.huayan.driverclient",
                            photoFile);
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE_SAVE);
                }
            } else if (mode == REQUEST_IMAGE_CAPTURE) {
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

    /**
     * 选择图片
     *
     * @param v
     */
    public void selectImg(View v) {
//        调用系统图库:
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, GET_IMAGE);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        获取缩略图
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            if (data != null) {
                Bundle bundle = data.getExtras();
                Bitmap bitmap = (Bitmap) bundle.get("data");
                ((ImageView) findViewById(R.id.takephoto)).setImageBitmap(bitmap);
//                BitmapFactory.Options o=new BitmapFactory.Options();
//                o.inScreenDensity=123;
//                ((ImageView) findViewById(R.id.takephoto)).setImage;
            } else {
                Toast.makeText(this, "返回的data为null", Toast.LENGTH_SHORT).show();
            }
        }
//        获取保存的图片
        if (requestCode == REQUEST_IMAGE_CAPTURE_SAVE && resultCode == RESULT_OK) {

        }
//        获取选择的图片
        if (requestCode == GET_IMAGE && resultCode == RESULT_OK) {
            Uri uri = data.getData();
            Cursor cursor = getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
                Log.i(TAG, "onActivityResult: imagepath:" + path);
            } else
                Log.i(TAG, "onActivityResult: imagepath:null");
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_IMAGE_CAPTURE) {
            for (String s : permissions) {
                Log.i(TAG, "onRequestPermissionsResult: " + s);
            }

            if (CAMERA.equals(permissions[0])) {
                if (grantResults[0] == PERMISSION_GRANTED) {
                    takePhoto(REQUEST_IMAGE_CAPTURE_SAVE);
                } else {
                    Toast.makeText(this, "拒绝了相机权限的申请", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(this, "不是相机权限请求结果", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "没有权限结果", Toast.LENGTH_SHORT).show();
        }
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值