Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Access Camera in Android 6.0 Marshmallow?

I am to new to Android 6.0 Coding Please Provide a solutions For the Below Code:

When i provide Run Time Permissions like READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE it shows an Exception like

java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.motorola.camera/.Camera clip={text/uri-list U:file:///storage/emulated/0/Pictures/MyAppNew%20File%20Upload/IMG_20160401_110234.jpg} (has extras) } from ProcessRecord{ed96564 26955:com.social.nocializer/u0a259} (pid=26955, uid=10259) with revoked permission android.permission.CAMERA

Either MediaStore.ACTION_IMAGE_CAPTURE and MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE Run Time Permissions are not working...

Note: READ_EXTERNAL_STORAGE Works For When Opening Gallery

like image 616
Jagan Avatar asked Apr 01 '16 06:04

Jagan


People also ask

What is the permission for using Camera?

Manifest declarations Camera Permission - Your application must request permission to use a device camera. Note: If you are using the camera by invoking an existing camera app, your application does not need to request this permission. For a list of camera features, see the manifest Features Reference.

What is Android controller permissions?

What is the Android permissions controller? The Android permissions controller is a part of the Android operating system that tells apps what they can and can't access. When you install a new app, the Android permissions controller is what gives you the option to allow or deny permissions for that app.


2 Answers

You have to manage run time permission for this, Because whichever permissions you have defined in AndroidManifest will not be automatically granted. So like below method you can check whether you permission is approved or not

if (checkSelfPermission(Manifest.permission.CAMERA)
        != PackageManager.PERMISSION_GRANTED) {

    requestPermissions(new String[]{Manifest.permission.CAMERA},
            MY_REQUEST_CODE);
}

Here, MY_REQUEST_CODE is a static constant that you can define, which will be used again for the requestPermission dialog callback. Now, You will need a callback for the dialog result:

@Override
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == MY_REQUEST__CODE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Now user should be able to use camera
        }
        else {
            // Your app will not have this permission. Turn off all functions 
            // that require this permission or it will force close like your 
            // original question
        }
    }
}
like image 60
Ronak Joshi Avatar answered Sep 30 '22 20:09

Ronak Joshi


@Ronak Solution worked for me but with a few following changes, As we need to check on only those devices which are above Android M.

 if( ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        requestPermissions(new String[]{android.Manifest.permission.CAMERA},
                                5);
                    }
                }

And override the following method using crl+o copying, pasting would might result in an error :D

 @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 5) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Now user should be able to use camera
            }
            else {
                // Your app will not have this permission. Turn off all functions
                // that require this permission or it will force close like your
                // original question
            }
    }
like image 26
cammando Avatar answered Sep 30 '22 18:09

cammando