摄像头选择内容未在Android M上启动

摄像头选择内容未在Android M上启动,android,android-camera,Android,Android Camera,我无法从Galery和Camera应用程序中拾取图像。我跟在后面。问题是摄像头选择内容没有在Android M上发布,其他版本正在发布,但Galery的目的是发布。那么如何修复它呢 这是选择内容: public static Intent getPickImageIntent(Context context) { Intent chooserIntent = null; List<Intent> intentList = new ArrayList&

我无法从Galery和Camera应用程序中拾取图像。我跟在后面。问题是摄像头选择内容没有在Android M上发布,其他版本正在发布,但Galery的目的是发布。那么如何修复它呢

这是选择内容:

public static Intent getPickImageIntent(Context context) {
        Intent chooserIntent = null;

        List<Intent> intentList = new ArrayList<>();

        Intent pickIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        takePhotoIntent.putExtra("return-data", true);
        takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context)));
        intentList = addIntentsToList(context, intentList, pickIntent);
        intentList = addIntentsToList(context, intentList, takePhotoIntent);

        if (intentList.size() > 0) {
            chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1),
                    context.getString(R.string.pick_image_intent_text));
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));
        }

    return chooserIntent;
}

对于开放式摄像机,您可以使用它

 static final int CAMERA_REQ_CODE = 1;

 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

 fileUri = Uri.fromFile(yourFile);

 intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

 startActivityForResult(intent, CAMERA_REQ_CODE);
编辑:运行时权限只是一个问题。我想知道为什么你的应用程序没有崩溃,当你没有要求相机的许可,我明白了。这是因为你从不打开相机:

chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1),
                context.getString(R.string.pick_image_intent_text));
您删除了intentList中的最后一个意图,即takePhotoIntent。因此intentList只包含pickIntent。这就是为什么

安卓M随附。某些权限需要由用户授予,应用程序才能使用它们。在这种情况下,它们是摄像头和外部存储器。因此,您必须请求权限:

public void onPickImage(View view) {
    askForCameraPermission();
}

private void askForCameraPermission() {
    // Neither CAMERA or WRITE_EXTERNAL_STORAGE is granted
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED
            && ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.CAMERA)
                || ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Permissions needed");
            builder.setMessage("Some explanations");
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    // User understands the situation, now ask for permissions
                    ActivityCompat.requestPermissions(SimpleActivity.this,
                            new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
                            PERMISSIONS_REQUEST_CAMERA);
                }
            });
            builder.show();

        } else {

            // No explanation needed, we can request the permission.

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

        // Permissions are already granted before
        Intent chooseImageIntent = ImagePicker.getPickImageIntent(this);
        startActivityForResult(chooseImageIntent, PICK_IMAGE_ID);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case PERMISSIONS_REQUEST_CAMERA: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay!
                Intent chooseImageIntent = ImagePicker.getPickImageIntent(this);
                startActivityForResult(chooseImageIntent, PICK_IMAGE_ID);

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.

                // Blame user
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Error");
                builder.setMessage("Y u do dis?!");
                builder.setPositiveButton("Nah", null);
                builder.show();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

在安卓M中,运行时权限发生了重大变化。为了使用相机,您必须在运行时授予权限。看看这个链接,它解释了如何处理权限请求流:


仍未启动照相机:
public void onPickImage(View view) {
    askForCameraPermission();
}

private void askForCameraPermission() {
    // Neither CAMERA or WRITE_EXTERNAL_STORAGE is granted
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED
            && ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.CAMERA)
                || ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Permissions needed");
            builder.setMessage("Some explanations");
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    // User understands the situation, now ask for permissions
                    ActivityCompat.requestPermissions(SimpleActivity.this,
                            new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
                            PERMISSIONS_REQUEST_CAMERA);
                }
            });
            builder.show();

        } else {

            // No explanation needed, we can request the permission.

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

        // Permissions are already granted before
        Intent chooseImageIntent = ImagePicker.getPickImageIntent(this);
        startActivityForResult(chooseImageIntent, PICK_IMAGE_ID);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case PERMISSIONS_REQUEST_CAMERA: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay!
                Intent chooseImageIntent = ImagePicker.getPickImageIntent(this);
                startActivityForResult(chooseImageIntent, PICK_IMAGE_ID);

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.

                // Blame user
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Error");
                builder.setMessage("Y u do dis?!");
                builder.setPositiveButton("Nah", null);
                builder.show();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}