如何解决相机许可?

时间:2019-05-21 06:44:07

标签: android android-sqlite

我在这里使用了所有这些活动,但是现在仍然无法正常工作。如果使用此代码还有其他解决方案或解决了此问题,请帮助我。我正在尝试从相机捕获图像并从图库中获取图像并进行显示。但是不幸的是,清单上始终有相同的问题允许拒绝。谢谢进阶

private static final int CAMERA_REQUEST = 101;
private static final int GALLERY_REQUEST = 102;

Uri imageUri;
final static String ARRAY_BYTE = "ARRAY_BYTE";

//on create

    imagecameraButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openCamera();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermission();
            }
        }

    });

    imagegalaryButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openGallery();
        }
    });

//permission


public void requestPermission() {
    if (ContextCompat.checkSelfPermission((ProductAddStep3Activity) context,
            Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale((ProductAddStep3Activity) this.context,
                Manifest.permission.CAMERA)) {
            ActivityCompat.requestPermissions((ProductAddStep3Activity) this.context,
                    new String[]{Manifest.permission.CAMERA}, Constants.PERMISSION_REQUEST_CAMERA);
        } else {
            //This will request for permisson
            ActivityCompat.requestPermissions((ProductAddStep3Activity) this.context,
                    new String[]{Manifest.permission.CAMERA}, Constants.PERMISSION_REQUEST_CAMERA);
        }
    }
}
//Callback for requestPermissions
@Override
public void onRequestPermissionsResult(int requestCode, String[] permission, int[] grantResults) {

    switch (requestCode) {
        case Constants.PERMISSION_REQUEST_CAMERA: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.i("open camera permission has been granted");
                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            } else {
                Log.i("open camera permission has been denied");
                //Dont call camera intent app will crash
            }
            return;
        }
    }
}

private void openGallery() {
    Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery, GALLERY_REQUEST);

}

private void openCamera() {

        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

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

    if (resultCode == RESULT_CANCELED) return;

    if (requestCode == CAMERA_REQUEST) {
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        imagephotoButton.setImageBitmap(bitmap);
    } else if (requestCode == GALLERY_REQUEST) {
        imageUri = data.getData();
        imagephotoButton.setImageURI(imageUri);

    }
}

}

2 个答案:

答案 0 :(得分:2)

尝试在openCamera函数中设置权限。另外,请改用WRITE_EXTERNAL_STORAGE

void openCamera(){
        if (ContextCompat.checkSelfPermission(
                getApplication(),
                Manifest.permission.WRITE_EXTERNAL_STORAGE
                    ) == PackageManager.PERMISSION_GRANTED
                ) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, CAMERA_REQUEST);
        } else {
            ActivityCompat.requestPermissions((ProductAddStep3Activity) this.context,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, Constants.PERMISSION_REQUEST_CAMERA);

        }
    }

答案 1 :(得分:0)

  

创建目录(外部路径),在该路径上创建文件,然后   将uri载入活动结果。

  Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(MediaStore.EXTRA_FINISH_ON_COMPLETION, true);
            if (cameraIntent.resolveActivity(Objects.requireNonNull(getContext()).getPackageManager()) != null) {
                pictureFilePath = getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/"
                        + ".jpg";
                File pictureFile = new File(pictureFilePath);
                if (pictureFile != null) {
                    Uri photoURI = FileProvider.getUriForFile(getContext(),
                            Constants.FILE_PROVIDER_IDENTIFIER,
                            pictureFile);
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                    startActivityForResult(cameraIntent, PICTURE_TAKEN_FROM_CAMERA);
                } else {
                    Toast.makeText(getActivity(), Constants.CANT_CREATE_PICTURE_MSG, Toast.LENGTH_LONG).show();
                }
            }

活动结果

 if (resultCode == Activity.RESULT_OK) {
                    File imgFile = new File(pictureFilePath);
                    if (imgFile.exists()) {
                        Uri uri = Uri.fromFile(imgFile);
                         }