Android 打开相机权限被拒绝

Posted

技术标签:

【中文标题】Android 打开相机权限被拒绝【英文标题】:Android Open Camera Permission Denial 【发布时间】:2018-09-08 08:59:53 【问题描述】:

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

java.lang.SecurityException: Permission Denial: 从 ProcessRecordb42fcde 29884:ukmutilizer.project.com.ukm_utilizer 开始 Intent act=android.media.action.IMAGE_CAPTURE cmp=com.sonyericsson.android.camera/.MultiWindowActivity /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;

【问题讨论】:

你的安卓是什么版本的? ***.com/a/49534463/115145 您是否授予了相机的运行时权限? How to Access Camera in Android 6.0 Marshmallow?的可能重复 【参考方案1】:

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

喜欢

在清单中

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

【讨论】:

【参考方案2】:

用户必须确认权限请求。 试试

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:

[...]
        
    

【讨论】:

【参考方案3】:

确保您向用户请求运行时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 中声明。

【讨论】:

如果它解决了您的问题,请接受答案。所以未来的用户可以马上使用这个解决方案!!【参考方案4】:

当您像棉花糖设备一样在 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);
                
            

【讨论】:

以上是关于Android 打开相机权限被拒绝的主要内容,如果未能解决你的问题,请参考以下文章

Android - 在没有提示的情况下拒绝相机权限

打开失败:EACCES(权限被拒绝)ANDROID 11

React Native saveToCameraRoll 在 Android 上因“权限被拒绝”错误而失败

Android 11 打开失败:EACCES(权限被拒绝)

打开失败:Android 中的 EACCES(权限被拒绝)

Android Intent FileProvider“权限被拒绝”在谷歌地球中打开KML文件?