Android M相机安全例外

时间:2016-04-04 10:11:11

标签: android android-camera android-permissions android-6.0-marshmallow

我可以选择让用户在我的活动中自拍。这是我的相同代码:

int REQUEST_CAMERA = 0, SELECT_FILE = 1;
private static final int REQUEST_SELFIE = 123;
private static String[] PERMISSIONS_SELFIE = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
LinearLayout selfie = (LinearLayout) findViewById(R.id.selfie);
        selfie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ((ContextCompat.checkSelfPermission(SelfieActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) &&
                        (ContextCompat.checkSelfPermission(SelfieActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
                        && (ContextCompat.checkSelfPermission(SelfieActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)) {
                    requestCameraAndExternalPermission();
                } else {
                    takeSelfie();
                }
            }
        });

private void requestCameraAndExternalPermission() {
        ActivityCompat.requestPermissions(this, PERMISSIONS_SELFIE, REQUEST_SELFIE);
    }

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_SELFIE) {
            if (grantResults.length == 3 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                    && grantResults[1] == PackageManager.PERMISSION_GRANTED
                    && grantResults[2] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
                takeSelfie();
            } else {
                Toast.makeText(getApplicationContext(), "Some Permissions Denied", Toast.LENGTH_SHORT).show();
            }
        } else if (requestCode == REQUEST_ALBUM) {
            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
                openGallery();
            } else {
                Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

private void takeSelfie() {
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
        if (file.exists()) {
            file.delete();
            file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
        }
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        startActivityForResult(intent, REQUEST_CAMERA);
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CAMERA) {
                File destination = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
                imagePath = destination.getAbsolutePath();
                Intent intent = new Intent(SelfieActivity.this, PhotoAdjustActivity.class);
                intent.putExtra("path", imagePath);
                startActivity(intent);
            } else if (requestCode == SELECT_FILE && data != null) {
                    Uri selectedImageUri = data.getData();
                    String[] projection = {MediaStore.MediaColumns.DATA};
                    Cursor cursor = getContentResolver().query(selectedImageUri, projection, null, null,
                            null);
                    assert cursor != null;
                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                    cursor.moveToFirst();
                    imagePath = cursor.getString(column_index);
                    if (imagePath == null) {
                        imagePath = getActualPathFromUri(selectedImageUri);
                    }
                    Intent intent = new Intent(SelfieActivity.this, PhotoAdjustActivity.class);
                    intent.putExtra("path", imagePath);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    startActivity(intent);
                    overridePendingTransition(0, 0);
                    cursor.close();
            }
        }
    }

当我查看我的手机(Lenovo K3 Note)时,它运行正常。但今天我在摩托罗拉(Android 6)遭遇了崩溃。这是崩溃:

Fatal Exception: java.lang.SecurityException
Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.motorola.camera/.Camera clip={text/uri-list U:file:///storage/emulated/0/image.jpg} (has extras) } from ProcessRecord{735d251 15134:com.galleri5.android/u0a149} (pid=15134, uid=10149) with revoked permission android.permission.CAMERA

这是我的清单:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <uses-feature android:name="android.hardware.camera.autofocus"
        android:required="false"/>

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

    <uses-feature
        android:name="android.hardware.camera"
        android:required="false" />

这可能是什么原因,我应该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

你的问题在onClick函数中,试试这个:

public void onClick(View v) {
                if ((ContextCompat.checkSelfPermission(SelfieActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) &&
                        (ContextCompat.checkSelfPermission(SelfieActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
                        && (ContextCompat.checkSelfPermission(SelfieActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)) {
                    requestCameraAndExternalPermission();
                } else {
                    if (grantResults.length == 3 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                    && grantResults[1] == PackageManager.PERMISSION_GRANTED
                    && grantResults[2] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
                takeSelfie();
                    } else {
                        Toast.makeText(getApplicationContext(), "Some Permissions Denied", Toast.LENGTH_SHORT).show();
                    }
                }
            }