请求权限的问题棉花糖 [英] Issues with requesting permissions Marshmallow

查看:67
本文介绍了请求权限的问题棉花糖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个基本的相机应用程序,可以从图库中访问保存的照片(需要作为另一个应用程序的一部分,但是由于问题,我一直在空白项目中进行开发)主要遵循本教程 https://guides.codepath.com/android/访问相机和存储媒体

I'm trying to make a basic camera application that can access the saved photo from the gallery (needed as part of another app but due to problems I've been having I am developing this in a blank project), and have been following mainly this tutorial https://guides.codepath.com/android/Accessing-the-Camera-and-Stored-Media

然后,我意识到由于权限在棉花糖中的工作方式以及需要向后兼容而导致崩溃,我尝试按照实现权限请求的教程进行操作,以便我可以实际使用该应用程序.

Having then realised that it would just crash because of how permissions work in Marshmallow, and needing backward compatibility, I've tried to follow tutorials on implementing permission requests so that I can actually use the app.

经过数小时的尝试,这是我目前拥有的.我已经在清单中添加了权限,但是由于这些权限是非常标准的,因此我无需费心将其复制并粘贴到上面.由于没有名为Storage的组,因此当前在test()方法上崩溃.注释掉该行之后,它只会说权限被拒绝,而不会提示我对权限进行排序(无论是否在棉花糖设备上).坦白说,我现在很茫然.我需要做的是在通过onLaunchCamera方法(通过单击按钮启动)启动相机之前,获得读取和写入外部存储以及访问相机的权限.您能提供的任何帮助将不胜感激.

This is what I currently have after several hours of trying. I have added permissions in the manifest, but as these are fairly standard I have not bothered to copy and paste these over. This currently crashes on the test() method because of there not being a group called Storage. With that line commented out, it will just say permission denied without prompting me to sort permissions (whether on a Marshmallow device or not). Frankly I am now at a loss. What I need this to do is before launching the camera in the onLaunchCamera method (which is launched off a button click), to get the permissions for reading and writing external storage and for accessing the camera. Any help you are able to give would be much appreciated.

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();
    }
}

推荐答案

尝试一下

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中设置了正确的版本

And make sure you have set proper version in your build.gradle

    **compileSdkVersion 23
    buildToolsVersion "23.0.2"**

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

    }

这篇关于请求权限的问题棉花糖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆