Android Open Camera权限拒绝

时间:2018-03-29 11:58:42

标签: android

我想在单击对话框上的项目时打开相机,但是我收到如下错误。我一直在为manifest.xml添加相机权限。

  

java.lang.SecurityException:Permission Denial:从ProcessRecord启动Intent {act = android.media.action.IMAGE_CAPTURE cmp = com.sonyericsson.android.camera / .MultiWindowActivity} {b42fcde 29884:ukmutilizer.project.com.ukm_utilizer / u0a273}(pid = 29884,uid = 10273)具有撤销权限android.permission.CAMERA

这是我的功能

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.personal_data_step, container, false);
    ButterKnife.bind(this, view);

    imageEktp.setClickable(true);

    imageEktp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Log.d("test : ", "testing");
            CharSequence menu[] = new CharSequence[]{"Take From Galery", "Open Camera"};
            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setTitle("Pick a Picture");
            builder.setItems(menu, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    if(i == 0){
                        Toast.makeText(getActivity(), "galery", Toast.LENGTH_SHORT).show();
                    }else{
                        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                        startActivity(intent);
                    }
                }
            });
            builder.show();
        }
    });

    return view;
}

4 个答案:

答案 0 :(得分:1)

确保您向用户请求运行时Camera Permission

private static final int CAMERA_REQUEST_CODE = 100;

如果未授予请求权限。

if (checkSelfPermission(Manifest.permission.CAMERA)
                        != PackageManager.PERMISSION_GRANTED) {
       requestPermissions(new String[]{Manifest.permission.CAMERA},
                            CAMERA_REQUEST_CODE);
}

onRequestPermissionsResult内填写内容。

@Override

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == CAMERA_REQUEST_CODE) {

            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Toast.makeText(this, "Camera permission granted.", Toast.LENGTH_LONG).show();

               // Do stuff here for Action Image Capture.

            } else {

                Toast.makeText(this, "Camera permission denied.", Toast.LENGTH_LONG).show();

            }

        }
    }

此外,这也应在Manifest内声明。

答案 1 :(得分:1)

用户必须确认许可请求 试试

public class ActivityMain extends AppCompatActivity
{

    public static final int MY_PERMISSIONS_REQUEST = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        if (ContextCompat.checkSelfPermission(ActivityMain.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
        {   ActivityCompat.requestPermissions(ActivityMain.this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST);
        }

[...]

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
    {   switch (requestCode)
        {   case MY_PERMISSIONS_REQUEST:

[...]
        }
    }
}

答案 2 :(得分:1)

当您在6.0以上的应用程序中运行您的应用程序,如棉花糖设备,然后需要许可,其他明智的,无需许可。 那段时间你的代码工作..

如果运行marshmallow设备那个时间需要许可然后在下面编写代码..

private void alertDialog(){
    CharSequence menu[] = new CharSequence[]{"Take From Galery", "Open Camera"};
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Pick a Picture");
    builder.setItems(menu, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            if(i == 0){
                Toast.makeText(getApplicationContext(), "galery", Toast.LENGTH_SHORT).show();
            }else{
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                startActivity(intent);
            }
        }
    });
    builder.show();

}

然后上面的方法放入如下的权限代码..

if (ContextCompat.checkSelfPermission(webView.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(webView.this, Manifest.permission.CAMERA)) {

                    alertDialog();
                }
                else{
                    ActivityCompat.requestPermissions(webView.this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
                }
            }

在清单文件中添加两个权限..

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

您的错误主要问题是权限,如果您添加以下行是工作。

if (ContextCompat.checkSelfPermission(webView.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(webView.this, Manifest.permission.CAMERA)) {

                    alertDialog();
                }
                else{
                    ActivityCompat.requestPermissions(webView.this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
                }
            }

答案 3 :(得分:0)

您必须获得运行时权限,然后才能访问您的相机

喜欢

清单中的

<uses-permission android:name="android.permission.CAMERA" />
Java中的

private static final int MY_CAMERA_REQUEST_CODE = 100;

     private void processPickImage() {
                if(hasCameraPermission()) {
                    pickImage();
                }
                else
                {
                    requestCameraPermission();
                }            
        }

        private boolean hasCameraPermission() {
            return ContextCompat.checkSelfPermission(context,
                    Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
        }

        @TargetApi(Build.VERSION_CODES.M)
        private void requestCameraPermission() {
            requestPermissions(new String[]{Manifest.permission.CAMERA},
                    MY_CAMERA_REQUEST_CODE );
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                               @NonNull int[] grantResults) {
            switch (requestCode) {
                case MY_CAMERA_REQUEST_CODE :

                    processPickImage();
                    break;
                default:
                    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }