简体   繁体   中英

Permission Denial in android while working with camera

I am newbie to native android development. I am working on android studio . I have a tabbed activity in which i am using 2 tabs. In one tab there is a map with marker on gps location of the user. The second tab is to take picture and then to save it in the storage provided by the OS. For this i followed this tutorial , run the app on my device but it's crashes. I am running it on Marshmallow . Below is the error i get

java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE cmp=android/com.android.internal.app.ResolverActivity } from ProcessRecord{e8f9820 15070:com.example.accurat.faisal/u0a228} (pid=15070, uid=10228) with revoked permission android.permission.CAMERA
                                                                                at android.os.Parcel.readException(Parcel.java:1620)
                                                                                at android.os.Parcel.readException(Parcel.java:1573)
                                                                                at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3181)
                                                                                at android.app.Instrumentation.execStartActivity(Instrumentation.java:1541)
                                                                                at android.app.Activity.startActivityForResult(Activity.java:4298)
                                                                                at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
                                                                                at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
                                                                                at android.support.v4.app.ActivityCompatJB.startActivityForResult(ActivityCompatJB.java:30)
                                                                                at android.support.v4.app.ActivityCompat.startActivityForResult(ActivityCompat.java:146)
                                                                                at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:937)
                                                                                at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:1047)
                                                                                at android.support.v4.app.Fragment.startActivityForResult(Fragment.java:954)
                                                                                at android.support.v4.app.Fragment.startActivityForResult(Fragment.java:943)
                                                                                at com.example.accurat.faisal.Camera$1.onClick(Camera.java:51)
                                                                                at android.view.View.performClick(View.java:5716)
                                                                                at android.widget.TextView.performClick(TextView.java:10926)
                                                                                at android.view.View$PerformClick.run(View.java:22596)
                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:148)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:7325)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

I searched it and found that i should ask permission for it so i followed this link and tried to add permission check like as bellow

 button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Check permission for CAMERA
            if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED) {

                // Check Permissions Now
                // Callback onRequestPermissionsResult interceptado na Activity MainActivity
                ActivityCompat.requestPermissions(getActivity(),
                        new String[]{Manifest.permission.CAMERA},
                        MainActivity.REQUEST_CAMERA);


            }

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE );
        }
    });

But i am getting red text of CAMERA and REQUEST_CAMERA . Though i have imported them as well but still the red text is showing

import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

Below is my manifest file

 <uses-permission android:name="com.example.accurat.faisal.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.hardware.camera"/>

I don't know what is the problem and don't know the solution and i am stuck to it.

Any help would be highly appreciated

You need to add permission check in your code and based on user action of accecpt or denial should show the camera preview Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running . Also, you need to ask user permission for android.Manifest.permission.WRITE_EXTERNAL_STORAGE if you are creating a file which will be used as an input for image(captured via camera).

You must use:

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

If you are using API 23+, because CAMERA permission is defined as a Dangerous permission, see here: https://developer.android.com/guide/topics/permissions/requesting.html#normal-dangerous

The user must accept the permission at runtime. You should check the intent result:

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

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

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

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

https://developer.android.com/training/permissions/requesting.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM