2
votes

I want to open camera when clicking an item on my dialog box, but I get an error like below. I've been adding a camera permission to manifest.xml.

java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE cmp=com.sonyericsson.android.camera/.MultiWindowActivity } from ProcessRecord{b42fcde 29884:ukmutilizer.project.com.ukm_utilizer/u0a273} (pid=29884, uid=10273) with revoked permission android.permission.CAMERA

this is my function

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
what version of android do you have? - godot
Are you giving run time permission for Camera ? - Praveen Rawat

4 Answers

1
votes

You have to get runtime permission then only access your camera

like

in manifest

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

in 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);
            }
        }
1
votes

The user has to confirm the permission request.
Try

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:

[...]
        }
    }
}
1
votes

Ensure you request Runtime Camera Permission from the user.

private static final int CAMERA_REQUEST_CODE = 100;

Request permission if not granted.

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

Do stuff inside 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();

            }

        }
    }

Also this should be declared inside Manifest as well.

1
votes

when you run your app in 6.0 above like marshmallow device then need permission other wise no need to permission. that time your code work..

if run marshmallow device that time need permission then make below code ..

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

}

then above method put in permission code like below ..

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

add two permission into manifest file ..

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

your error main problem is permission if you add this below line is work.

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