摄像头意图许可,android,为什么这不起作用?

摄像头意图许可,android,为什么这不起作用?,android,android-permissions,Android,Android Permissions,对清单使用权限 <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-p

对清单使用权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
这是用于其他方法的(您可以忽略)

当代码用于像这样从库中获取图像时,代码运行良好

private void startGallery() {
    Intent cameraIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    cameraIntent.setType("image/*");
    if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
        startActivityForResult(cameraIntent, 2000);
    }
}
所以,我认为问题在于android的许可

代码在android 5.xx或同等版本下运行良好

但不要在超过或等于android 6.xx的版本上工作

问题:我是否遗漏了任何权限失败的代码?

Android权限太难理解您能告诉我如何修改此代码吗?

编辑

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
“不工作”的意思是:如果我单击mCameraButton,则不会发生任何事情。什么也没发生

所以,我认为问题在于android的许可

代码在android 5.xx或同等版本下运行良好

但不要在超过或等于android 6.xx的版本上工作

为此,您必须为Android 6.0及更高版本添加运行时权限

例如

final private int REQUEST_CODE_ASK_PERMISSIONS_CAMERA = 100;
final private int REQUEST_CODE_ASK_PERMISSIONS_EXTERNAL_STORAGE = 200;

// For Check Camera Permission
if (Build.VERSION.SDK_INT >= 23) {
            int hasPermission = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA);
            if (hasPermission != PackageManager.PERMISSION_GRANTED) {
                if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                    // Display UI and wait for user interaction
                    getErrorDialog("You need to allow Camera permission." +
                            "\nIf you disable this permission, You will not able to add attachment.", getActivity(), true).show();
                } else {
                    requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_ASK_PERMISSIONS_CAMERA);
                }
                return;
            }
        }

// For Check Read External Permission.
if (Build.VERSION.SDK_INT >= 23) {
            int hasPermission = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
            if (hasPermission != PackageManager.PERMISSION_GRANTED) {
                if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    // Display UI and wait for user interaction
                    getErrorDialog("You need to allow Read External Storage permission." +
                            "\nIf you disable this permission, You will not able to add attachment.", getActivity(), false).show();
                } else {
                    requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_ASK_PERMISSIONS_EXTERNAL_STORAGE);
                }
                return;
            }
        }

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_PERMISSIONS_CAMERA:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission Granted
                    Toast.makeText(getActivity(), "Permission Grant", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                    String imageFileName = "JPEG_" + timeStamp + ".jpg";
                    File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), imageFileName);
                    uri = Uri.fromFile(imageStorageDir);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                    startActivityForResult(intent, 1);

                } else {
                    // Permission Denied
                    Toast.makeText(getActivity(), "Required permission is disable.", Toast.LENGTH_SHORT).show();
                }
                break;

            case REQUEST_CODE_ASK_PERMISSIONS_EXTERNAL_STORAGE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission Granted
                    Toast.makeText(getActivity(), "Permission Grant", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    startActivityForResult(intent, 2);

                } else {
                    // Permission Denied
                    Toast.makeText(getActivity(), "Required permission is disable.", Toast.LENGTH_SHORT).show();
                }
                break;

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

public AlertDialog.Builder getErrorDialog(String message, Context context, final boolean isFromCamera) {
        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
        alertDialog.setTitle(getString(R.string.app_name)).setMessage(message);
        alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();
                if (Build.VERSION.SDK_INT >= 23) {
                    if(isFromCamera){
                        requestPermissions(new String[]{Manifest.permission.CAMERA},
                                REQUEST_CODE_ASK_PERMISSIONS_CAMERA);
                    }else {
                        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                                REQUEST_CODE_ASK_PERMISSIONS_EXTERNAL_STORAGE);
                    }
                }

            }
        });
        return alertDialog;
    }
所以,我认为问题在于android的许可

代码在android 5.xx或同等版本下运行良好

但不要在超过或等于android 6.xx的版本上工作

为此,您必须为Android 6.0及更高版本添加运行时权限

例如

final private int REQUEST_CODE_ASK_PERMISSIONS_CAMERA = 100;
final private int REQUEST_CODE_ASK_PERMISSIONS_EXTERNAL_STORAGE = 200;

// For Check Camera Permission
if (Build.VERSION.SDK_INT >= 23) {
            int hasPermission = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA);
            if (hasPermission != PackageManager.PERMISSION_GRANTED) {
                if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                    // Display UI and wait for user interaction
                    getErrorDialog("You need to allow Camera permission." +
                            "\nIf you disable this permission, You will not able to add attachment.", getActivity(), true).show();
                } else {
                    requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_ASK_PERMISSIONS_CAMERA);
                }
                return;
            }
        }

// For Check Read External Permission.
if (Build.VERSION.SDK_INT >= 23) {
            int hasPermission = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
            if (hasPermission != PackageManager.PERMISSION_GRANTED) {
                if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    // Display UI and wait for user interaction
                    getErrorDialog("You need to allow Read External Storage permission." +
                            "\nIf you disable this permission, You will not able to add attachment.", getActivity(), false).show();
                } else {
                    requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_ASK_PERMISSIONS_EXTERNAL_STORAGE);
                }
                return;
            }
        }

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_PERMISSIONS_CAMERA:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission Granted
                    Toast.makeText(getActivity(), "Permission Grant", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                    String imageFileName = "JPEG_" + timeStamp + ".jpg";
                    File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), imageFileName);
                    uri = Uri.fromFile(imageStorageDir);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                    startActivityForResult(intent, 1);

                } else {
                    // Permission Denied
                    Toast.makeText(getActivity(), "Required permission is disable.", Toast.LENGTH_SHORT).show();
                }
                break;

            case REQUEST_CODE_ASK_PERMISSIONS_EXTERNAL_STORAGE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission Granted
                    Toast.makeText(getActivity(), "Permission Grant", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    startActivityForResult(intent, 2);

                } else {
                    // Permission Denied
                    Toast.makeText(getActivity(), "Required permission is disable.", Toast.LENGTH_SHORT).show();
                }
                break;

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

public AlertDialog.Builder getErrorDialog(String message, Context context, final boolean isFromCamera) {
        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
        alertDialog.setTitle(getString(R.string.app_name)).setMessage(message);
        alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();
                if (Build.VERSION.SDK_INT >= 23) {
                    if(isFromCamera){
                        requestPermissions(new String[]{Manifest.permission.CAMERA},
                                REQUEST_CODE_ASK_PERMISSIONS_CAMERA);
                    }else {
                        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                                REQUEST_CODE_ASK_PERMISSIONS_EXTERNAL_STORAGE);
                    }
                }

            }
        });
        return alertDialog;
    }

Android Marshmellow
开始,Android生态系统要求开发者在运行时获得一定的权限,这意味着他们应该明确要求用户授予权限。你可以读更多


但我知道请求这些权限需要编写大量样板代码,这会让人望而生畏。所以我建议你使用第三方图书馆。我最喜欢的是
Let
大大减少了使用运行时权限所需的样板文件。

Android Marshmellow
开始,Android生态系统要求开发人员在运行时获得特定权限,这意味着他们应该明确要求用户授予权限。你可以读更多


但我知道请求这些权限需要编写大量样板代码,这会让人望而生畏。所以我建议你使用第三方图书馆。我最喜欢的是
Let
大大减少了使用运行时权限所需的样板文件。

请求运行时权限很容易理解。 请查看Android官方在运行时请求权限


请求运行时权限很容易理解。 请查看Android官方在运行时请求权限


请详细解释“不起作用”的含义。另外,
ACTION\u IMAGE\u CAPTURE
不会返回
Uri
。您必须为Android 6.0及以上版本添加
运行时权限
。@Commonware我添加了“不工作”意味着什么也没发生。@ChiragSavsani我必须补充一点,在哪里,如何?你能给我解释一下吗?请参见下面的示例,该示例基于
片段
。如果您正在使用活动,则需要进行一些更改。例如,在Activity
ActivityCompat.requestPermissions(URActivity.this,…)
ActivityCompat.shouldShowRequestPermissionRegulation(Activity.this,…)
中,请详细解释“不起作用”的含义。另外,
ACTION\u IMAGE\u CAPTURE
不会返回
Uri
。您必须为Android 6.0及以上版本添加
运行时权限
。@Commonware我添加了“不工作”意味着什么也没发生。@ChiragSavsani我必须补充一点,在哪里,如何?你能给我解释一下吗?请参见下面的示例,该示例基于
片段
。如果您正在使用活动,则需要进行一些更改。例如,在Activity
ActivityCompat.requestPermissions(URActivity.this,…)
ActivityCompat.shouldRequestPermissionRegulation(Activity.this,…)
感谢您的善意回答Chirag Savsani感谢您的善意回答Chirag Savsani感谢您的善意回答Chirag Savsani