Where to place request permission for camera?












1















I need to request camera and location permission in some activity that show the camera preview in surfaceview.



Where do I need to put the request permission function? onCreate, onResume, or onSurfaceCreated?



And where do I need to setup the camera?










share|improve this question

























  • best way to put permission in onResume.

    – Vinesh Chauhan
    Jan 19 at 6:37











  • ideally you'd get permissions BEFORE you load the activity up but you can request it anywhere that doesn't interfere in the "flow" of your application. onResume is where most production apps place code as activity is destroyed when user changes orientation and onResume is guaranteed to be called

    – Karan Harsh Wardhan
    Jan 19 at 6:39
















1















I need to request camera and location permission in some activity that show the camera preview in surfaceview.



Where do I need to put the request permission function? onCreate, onResume, or onSurfaceCreated?



And where do I need to setup the camera?










share|improve this question

























  • best way to put permission in onResume.

    – Vinesh Chauhan
    Jan 19 at 6:37











  • ideally you'd get permissions BEFORE you load the activity up but you can request it anywhere that doesn't interfere in the "flow" of your application. onResume is where most production apps place code as activity is destroyed when user changes orientation and onResume is guaranteed to be called

    – Karan Harsh Wardhan
    Jan 19 at 6:39














1












1








1








I need to request camera and location permission in some activity that show the camera preview in surfaceview.



Where do I need to put the request permission function? onCreate, onResume, or onSurfaceCreated?



And where do I need to setup the camera?










share|improve this question
















I need to request camera and location permission in some activity that show the camera preview in surfaceview.



Where do I need to put the request permission function? onCreate, onResume, or onSurfaceCreated?



And where do I need to setup the camera?







java android android-camera surfaceview android-permissions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 8:02









Dhanshri

431210




431210










asked Jan 19 at 6:35









Reynaldo NapitupuluReynaldo Napitupulu

113




113













  • best way to put permission in onResume.

    – Vinesh Chauhan
    Jan 19 at 6:37











  • ideally you'd get permissions BEFORE you load the activity up but you can request it anywhere that doesn't interfere in the "flow" of your application. onResume is where most production apps place code as activity is destroyed when user changes orientation and onResume is guaranteed to be called

    – Karan Harsh Wardhan
    Jan 19 at 6:39



















  • best way to put permission in onResume.

    – Vinesh Chauhan
    Jan 19 at 6:37











  • ideally you'd get permissions BEFORE you load the activity up but you can request it anywhere that doesn't interfere in the "flow" of your application. onResume is where most production apps place code as activity is destroyed when user changes orientation and onResume is guaranteed to be called

    – Karan Harsh Wardhan
    Jan 19 at 6:39

















best way to put permission in onResume.

– Vinesh Chauhan
Jan 19 at 6:37





best way to put permission in onResume.

– Vinesh Chauhan
Jan 19 at 6:37













ideally you'd get permissions BEFORE you load the activity up but you can request it anywhere that doesn't interfere in the "flow" of your application. onResume is where most production apps place code as activity is destroyed when user changes orientation and onResume is guaranteed to be called

– Karan Harsh Wardhan
Jan 19 at 6:39





ideally you'd get permissions BEFORE you load the activity up but you can request it anywhere that doesn't interfere in the "flow" of your application. onResume is where most production apps place code as activity is destroyed when user changes orientation and onResume is guaranteed to be called

– Karan Harsh Wardhan
Jan 19 at 6:39












4 Answers
4






active

oldest

votes


















0














See asking permission directly corresponds to the app crash if the service is requested and the permission is not available.
I would prefer to put the permission in onResume because whatever user action become i.e. like minimizing or a battery low dialog comes on top of screen, we need to check the permission changes again so that you activity changes might have occured through what ever reasons. Either camera is trying to open after network request or what the situation be.



Put the camera check permission in onResume.



So lets talk about how would you do it.
There are a number of perceptions. What I prefer to do is
I create a Helper class that would let me know the permission status with this code



class PermissionsHelper(activity: Activity) {
private val activity: Context

init { this.activity = activity }

fun isCameraPermissionAvailable()=ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED


}



}



So in your activity's onResume method check if the permission is available else request the permission.



 override fun onResume() {
super.onResume()
if (!PermissionsHelper(this).isCameraPermissionAvailable()) {
requestPermissions(arrayOf(Manifest.permission.CAMERA), CAMERA_REQUEST_CODE)
}
}


Also please note two points



1) You should write permission for camera in manifeast so that app can request the permission
2) Check if the camera permission is available or not before opening the camera, if not you should again request for the permission


(same as in onResume stage)






share|improve this answer































    0














    First of all, define the permission in the manifest file.



    In the java file,



    You must request the permission again at runtime using checkSelfPermission. This is quite necessary as if you the user hasn't given the permission he won't be able to access the camera. You should write this code and all the functionality related to the camera when you want the camera to actually show up.



    For example, if there is a button say Open Camera then first check for the permission if the user has granted or not and then open the camera.






    share|improve this answer































      0














      You can place the permissions anywhere before using the service. Technically it doesn't matter. But asking for permission with a start-up screen providing the reason why the permission is needed or asking for it just before the service is used is user-friendly.






      share|improve this answer































        0














        Perhaps the most relevant place to ask for permission as per Android documentation is




        every time you perform an operation that requires that permission.




        The reason behind such recommendation is Beginning with Android 6.0 (API level 23), users can revoke permissions from any app at any time. So even if the app used the camera yesterday, it can't assume it still has that permission today.



        And hence app must "check" every time for the permission, to perform an operation that requires that permission.



        You can setup Camera and other operations "after" the permission was granted to use the Camera by user. You can check the status if permission is granted by user by overriding onRequestPermissionsResult



        @Override
        public void onRequestPermissionsResult(int requestCode,
        String permissions, int grantResults) {
        switch (requestCode) {
        case MY_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! Do the
        // camera-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.
        }
        }





        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54264683%2fwhere-to-place-request-permission-for-camera%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          See asking permission directly corresponds to the app crash if the service is requested and the permission is not available.
          I would prefer to put the permission in onResume because whatever user action become i.e. like minimizing or a battery low dialog comes on top of screen, we need to check the permission changes again so that you activity changes might have occured through what ever reasons. Either camera is trying to open after network request or what the situation be.



          Put the camera check permission in onResume.



          So lets talk about how would you do it.
          There are a number of perceptions. What I prefer to do is
          I create a Helper class that would let me know the permission status with this code



          class PermissionsHelper(activity: Activity) {
          private val activity: Context

          init { this.activity = activity }

          fun isCameraPermissionAvailable()=ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED


          }



          }



          So in your activity's onResume method check if the permission is available else request the permission.



           override fun onResume() {
          super.onResume()
          if (!PermissionsHelper(this).isCameraPermissionAvailable()) {
          requestPermissions(arrayOf(Manifest.permission.CAMERA), CAMERA_REQUEST_CODE)
          }
          }


          Also please note two points



          1) You should write permission for camera in manifeast so that app can request the permission
          2) Check if the camera permission is available or not before opening the camera, if not you should again request for the permission


          (same as in onResume stage)






          share|improve this answer




























            0














            See asking permission directly corresponds to the app crash if the service is requested and the permission is not available.
            I would prefer to put the permission in onResume because whatever user action become i.e. like minimizing or a battery low dialog comes on top of screen, we need to check the permission changes again so that you activity changes might have occured through what ever reasons. Either camera is trying to open after network request or what the situation be.



            Put the camera check permission in onResume.



            So lets talk about how would you do it.
            There are a number of perceptions. What I prefer to do is
            I create a Helper class that would let me know the permission status with this code



            class PermissionsHelper(activity: Activity) {
            private val activity: Context

            init { this.activity = activity }

            fun isCameraPermissionAvailable()=ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED


            }



            }



            So in your activity's onResume method check if the permission is available else request the permission.



             override fun onResume() {
            super.onResume()
            if (!PermissionsHelper(this).isCameraPermissionAvailable()) {
            requestPermissions(arrayOf(Manifest.permission.CAMERA), CAMERA_REQUEST_CODE)
            }
            }


            Also please note two points



            1) You should write permission for camera in manifeast so that app can request the permission
            2) Check if the camera permission is available or not before opening the camera, if not you should again request for the permission


            (same as in onResume stage)






            share|improve this answer


























              0












              0








              0







              See asking permission directly corresponds to the app crash if the service is requested and the permission is not available.
              I would prefer to put the permission in onResume because whatever user action become i.e. like minimizing or a battery low dialog comes on top of screen, we need to check the permission changes again so that you activity changes might have occured through what ever reasons. Either camera is trying to open after network request or what the situation be.



              Put the camera check permission in onResume.



              So lets talk about how would you do it.
              There are a number of perceptions. What I prefer to do is
              I create a Helper class that would let me know the permission status with this code



              class PermissionsHelper(activity: Activity) {
              private val activity: Context

              init { this.activity = activity }

              fun isCameraPermissionAvailable()=ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED


              }



              }



              So in your activity's onResume method check if the permission is available else request the permission.



               override fun onResume() {
              super.onResume()
              if (!PermissionsHelper(this).isCameraPermissionAvailable()) {
              requestPermissions(arrayOf(Manifest.permission.CAMERA), CAMERA_REQUEST_CODE)
              }
              }


              Also please note two points



              1) You should write permission for camera in manifeast so that app can request the permission
              2) Check if the camera permission is available or not before opening the camera, if not you should again request for the permission


              (same as in onResume stage)






              share|improve this answer













              See asking permission directly corresponds to the app crash if the service is requested and the permission is not available.
              I would prefer to put the permission in onResume because whatever user action become i.e. like minimizing or a battery low dialog comes on top of screen, we need to check the permission changes again so that you activity changes might have occured through what ever reasons. Either camera is trying to open after network request or what the situation be.



              Put the camera check permission in onResume.



              So lets talk about how would you do it.
              There are a number of perceptions. What I prefer to do is
              I create a Helper class that would let me know the permission status with this code



              class PermissionsHelper(activity: Activity) {
              private val activity: Context

              init { this.activity = activity }

              fun isCameraPermissionAvailable()=ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED


              }



              }



              So in your activity's onResume method check if the permission is available else request the permission.



               override fun onResume() {
              super.onResume()
              if (!PermissionsHelper(this).isCameraPermissionAvailable()) {
              requestPermissions(arrayOf(Manifest.permission.CAMERA), CAMERA_REQUEST_CODE)
              }
              }


              Also please note two points



              1) You should write permission for camera in manifeast so that app can request the permission
              2) Check if the camera permission is available or not before opening the camera, if not you should again request for the permission


              (same as in onResume stage)







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 19 at 7:00









              Sháilèndra WregmiSháilèndra Wregmi

              295127




              295127

























                  0














                  First of all, define the permission in the manifest file.



                  In the java file,



                  You must request the permission again at runtime using checkSelfPermission. This is quite necessary as if you the user hasn't given the permission he won't be able to access the camera. You should write this code and all the functionality related to the camera when you want the camera to actually show up.



                  For example, if there is a button say Open Camera then first check for the permission if the user has granted or not and then open the camera.






                  share|improve this answer




























                    0














                    First of all, define the permission in the manifest file.



                    In the java file,



                    You must request the permission again at runtime using checkSelfPermission. This is quite necessary as if you the user hasn't given the permission he won't be able to access the camera. You should write this code and all the functionality related to the camera when you want the camera to actually show up.



                    For example, if there is a button say Open Camera then first check for the permission if the user has granted or not and then open the camera.






                    share|improve this answer


























                      0












                      0








                      0







                      First of all, define the permission in the manifest file.



                      In the java file,



                      You must request the permission again at runtime using checkSelfPermission. This is quite necessary as if you the user hasn't given the permission he won't be able to access the camera. You should write this code and all the functionality related to the camera when you want the camera to actually show up.



                      For example, if there is a button say Open Camera then first check for the permission if the user has granted or not and then open the camera.






                      share|improve this answer













                      First of all, define the permission in the manifest file.



                      In the java file,



                      You must request the permission again at runtime using checkSelfPermission. This is quite necessary as if you the user hasn't given the permission he won't be able to access the camera. You should write this code and all the functionality related to the camera when you want the camera to actually show up.



                      For example, if there is a button say Open Camera then first check for the permission if the user has granted or not and then open the camera.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 19 at 6:46









                      Sagar BalyanSagar Balyan

                      217




                      217























                          0














                          You can place the permissions anywhere before using the service. Technically it doesn't matter. But asking for permission with a start-up screen providing the reason why the permission is needed or asking for it just before the service is used is user-friendly.






                          share|improve this answer




























                            0














                            You can place the permissions anywhere before using the service. Technically it doesn't matter. But asking for permission with a start-up screen providing the reason why the permission is needed or asking for it just before the service is used is user-friendly.






                            share|improve this answer


























                              0












                              0








                              0







                              You can place the permissions anywhere before using the service. Technically it doesn't matter. But asking for permission with a start-up screen providing the reason why the permission is needed or asking for it just before the service is used is user-friendly.






                              share|improve this answer













                              You can place the permissions anywhere before using the service. Technically it doesn't matter. But asking for permission with a start-up screen providing the reason why the permission is needed or asking for it just before the service is used is user-friendly.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jan 19 at 7:15









                              Charan MCharan M

                              34917




                              34917























                                  0














                                  Perhaps the most relevant place to ask for permission as per Android documentation is




                                  every time you perform an operation that requires that permission.




                                  The reason behind such recommendation is Beginning with Android 6.0 (API level 23), users can revoke permissions from any app at any time. So even if the app used the camera yesterday, it can't assume it still has that permission today.



                                  And hence app must "check" every time for the permission, to perform an operation that requires that permission.



                                  You can setup Camera and other operations "after" the permission was granted to use the Camera by user. You can check the status if permission is granted by user by overriding onRequestPermissionsResult



                                  @Override
                                  public void onRequestPermissionsResult(int requestCode,
                                  String permissions, int grantResults) {
                                  switch (requestCode) {
                                  case MY_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! Do the
                                  // camera-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.
                                  }
                                  }





                                  share|improve this answer




























                                    0














                                    Perhaps the most relevant place to ask for permission as per Android documentation is




                                    every time you perform an operation that requires that permission.




                                    The reason behind such recommendation is Beginning with Android 6.0 (API level 23), users can revoke permissions from any app at any time. So even if the app used the camera yesterday, it can't assume it still has that permission today.



                                    And hence app must "check" every time for the permission, to perform an operation that requires that permission.



                                    You can setup Camera and other operations "after" the permission was granted to use the Camera by user. You can check the status if permission is granted by user by overriding onRequestPermissionsResult



                                    @Override
                                    public void onRequestPermissionsResult(int requestCode,
                                    String permissions, int grantResults) {
                                    switch (requestCode) {
                                    case MY_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! Do the
                                    // camera-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.
                                    }
                                    }





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      Perhaps the most relevant place to ask for permission as per Android documentation is




                                      every time you perform an operation that requires that permission.




                                      The reason behind such recommendation is Beginning with Android 6.0 (API level 23), users can revoke permissions from any app at any time. So even if the app used the camera yesterday, it can't assume it still has that permission today.



                                      And hence app must "check" every time for the permission, to perform an operation that requires that permission.



                                      You can setup Camera and other operations "after" the permission was granted to use the Camera by user. You can check the status if permission is granted by user by overriding onRequestPermissionsResult



                                      @Override
                                      public void onRequestPermissionsResult(int requestCode,
                                      String permissions, int grantResults) {
                                      switch (requestCode) {
                                      case MY_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! Do the
                                      // camera-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.
                                      }
                                      }





                                      share|improve this answer













                                      Perhaps the most relevant place to ask for permission as per Android documentation is




                                      every time you perform an operation that requires that permission.




                                      The reason behind such recommendation is Beginning with Android 6.0 (API level 23), users can revoke permissions from any app at any time. So even if the app used the camera yesterday, it can't assume it still has that permission today.



                                      And hence app must "check" every time for the permission, to perform an operation that requires that permission.



                                      You can setup Camera and other operations "after" the permission was granted to use the Camera by user. You can check the status if permission is granted by user by overriding onRequestPermissionsResult



                                      @Override
                                      public void onRequestPermissionsResult(int requestCode,
                                      String permissions, int grantResults) {
                                      switch (requestCode) {
                                      case MY_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! Do the
                                      // camera-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.
                                      }
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jan 19 at 7:26









                                      AADProgrammingAADProgramming

                                      2,96072149




                                      2,96072149






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54264683%2fwhere-to-place-request-permission-for-camera%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Liquibase includeAll doesn't find base path

                                          How to use setInterval in EJS file?

                                          Petrus Granier-Deferre