请求权限Marshmallow的问题

时间:2016-06-21 13:38:03

标签: android

我正在尝试制作一个基本的相机应用程序,可以从库中访问已保存的照片(需要作为另一个应用程序的一部分,但由于我遇到的问题,我正在一个空白的项目中开发这个),并且主要关注本教程https://guides.codepath.com/android/Accessing-the-Camera-and-Stored-Media

然后意识到它会因为Marshmallow中的权限如何工作而崩溃,并且需要向后兼容性,我试图遵循实施权限请求的教程,以便我可以实际使用该应用程序。

这是我经过几个小时的努力后目前所拥有的。我已经在清单中添加了权限,但由于这些是相当标准的,我没有费心去复制和粘贴这些权限。这当前在test()方法上崩溃,因为没有名为Storage的组。在该行被注释掉的情况下,它只会说权限被拒绝而不会提示我对权限进行排序(无论是否在Marshmallow设备上)。坦率地说,我现在不知所措。我需要做的是在onLaunchCamera方法(通过点击按钮启动)启动相机之前,获得读取和写入外部存储以及访问相机的权限。你能给予的任何帮助都会非常感激。

private boolean cameraPermissionsCheck() {
    return ContextCompat.checkSelfPermission(this, Manifest.permission_group.CAMERA) == PackageManager.PERMISSION_GRANTED;
}

private boolean storagePermissionsCheck() {
    return ContextCompat.checkSelfPermission(this, Manifest.permission_group.STORAGE) == PackageManager.PERMISSION_GRANTED;
}

private void requestPermissions() {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission_group.CAMERA, Manifest.permission_group.STORAGE}, 123);
}

private void test() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission_group.STORAGE)) {
        //was a toast notification here
        requestPermissions();
    } else {
        requestPermissions();
    }
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 123
            && grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
    }
}

public void onLaunchCamera(View view) {

    //btn = (Button) findViewById(R.id.button);
    if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
        if(!cameraPermissionsCheck() || !storagePermissionsCheck()){
            test();
        }
        else {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(photoFileName)); // set the image file name

            if (intent.resolveActivity(getPackageManager()) != null) {
            // Start the image capture intent to take photo
            startActivityForResult(intent, 0);
            }
        }
    } else {
        Toast.makeText(MainActivity.this, "No Camera",
                Toast.LENGTH_LONG).show();
    }
}

1 个答案:

答案 0 :(得分:1)

试试这个

public void onLaunchCamera(View view) {

    //btn = (Button) findViewById(R.id.button);
    if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
         if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
            checkPermission();
        }
        else {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(photoFileName)); // set the image file name

            if (intent.resolveActivity(getPackageManager()) != null) {
            // Start the image capture intent to take photo
            startActivityForResult(intent, 0);
            }
        }
    } else {
        Toast.makeText(MainActivity.this, "No Camera",
                Toast.LENGTH_LONG).show();
    }
}

private void checkPermission() {
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,
                    Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED) {//Can add more as per requirement

                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA},
                        123);

            } else {

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,getPhotoFileUri(photoFileName)); // set the image file name     

           if (intent.resolveActivity(getPackageManager()) != null) {
           // Start the image capture intent to take photo
           startActivityForResult(intent, 0);
             }
          }

并确保在build.gradle中设置了正确的版本

    **compileSdkVersion 23
    buildToolsVersion "23.0.2"**

    defaultConfig {
        applicationId "your_package_name"
        minSdkVersion 15
        **targetSdkVersion 23**
        versionCode 1
        versionName "1.0"
        multiDexEnabled true

    }