App doesn't respond for camera permission












0














This is a part of QR Code scanner app. In the very beginning of the app it asks for camera permission, when i click to allow, app doesn't respond. but from second time, app works correctly. I think the error may be in checking permission. Please help me to find the mistake.



surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {

cameraSource.start(surfaceView.getHolder());
} else {
ActivityCompat.requestPermissions(MainActivity.this, new
String{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
}

} catch (IOException e) {
e.printStackTrace();
}

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});









share|improve this question






















  • See this answer stackoverflow.com/a/53246376/7360848 and if it solves the problem, mark it as the accepted answer please :)
    – Touhidul Islam
    Nov 11 at 7:44










  • @TouhidulIslam , i'm not getting you. What should i do for my code?
    – Bikash Adhikari
    Nov 11 at 8:10










  • check my answer, you have to add onRequestPermissionsResult callback
    – Touhidul Islam
    Nov 11 at 8:18
















0














This is a part of QR Code scanner app. In the very beginning of the app it asks for camera permission, when i click to allow, app doesn't respond. but from second time, app works correctly. I think the error may be in checking permission. Please help me to find the mistake.



surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {

cameraSource.start(surfaceView.getHolder());
} else {
ActivityCompat.requestPermissions(MainActivity.this, new
String{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
}

} catch (IOException e) {
e.printStackTrace();
}

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});









share|improve this question






















  • See this answer stackoverflow.com/a/53246376/7360848 and if it solves the problem, mark it as the accepted answer please :)
    – Touhidul Islam
    Nov 11 at 7:44










  • @TouhidulIslam , i'm not getting you. What should i do for my code?
    – Bikash Adhikari
    Nov 11 at 8:10










  • check my answer, you have to add onRequestPermissionsResult callback
    – Touhidul Islam
    Nov 11 at 8:18














0












0








0







This is a part of QR Code scanner app. In the very beginning of the app it asks for camera permission, when i click to allow, app doesn't respond. but from second time, app works correctly. I think the error may be in checking permission. Please help me to find the mistake.



surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {

cameraSource.start(surfaceView.getHolder());
} else {
ActivityCompat.requestPermissions(MainActivity.this, new
String{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
}

} catch (IOException e) {
e.printStackTrace();
}

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});









share|improve this question













This is a part of QR Code scanner app. In the very beginning of the app it asks for camera permission, when i click to allow, app doesn't respond. but from second time, app works correctly. I think the error may be in checking permission. Please help me to find the mistake.



surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {

cameraSource.start(surfaceView.getHolder());
} else {
ActivityCompat.requestPermissions(MainActivity.this, new
String{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
}

} catch (IOException e) {
e.printStackTrace();
}

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});






android surfaceview






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 6:11









Bikash Adhikari

15




15












  • See this answer stackoverflow.com/a/53246376/7360848 and if it solves the problem, mark it as the accepted answer please :)
    – Touhidul Islam
    Nov 11 at 7:44










  • @TouhidulIslam , i'm not getting you. What should i do for my code?
    – Bikash Adhikari
    Nov 11 at 8:10










  • check my answer, you have to add onRequestPermissionsResult callback
    – Touhidul Islam
    Nov 11 at 8:18


















  • See this answer stackoverflow.com/a/53246376/7360848 and if it solves the problem, mark it as the accepted answer please :)
    – Touhidul Islam
    Nov 11 at 7:44










  • @TouhidulIslam , i'm not getting you. What should i do for my code?
    – Bikash Adhikari
    Nov 11 at 8:10










  • check my answer, you have to add onRequestPermissionsResult callback
    – Touhidul Islam
    Nov 11 at 8:18
















See this answer stackoverflow.com/a/53246376/7360848 and if it solves the problem, mark it as the accepted answer please :)
– Touhidul Islam
Nov 11 at 7:44




See this answer stackoverflow.com/a/53246376/7360848 and if it solves the problem, mark it as the accepted answer please :)
– Touhidul Islam
Nov 11 at 7:44












@TouhidulIslam , i'm not getting you. What should i do for my code?
– Bikash Adhikari
Nov 11 at 8:10




@TouhidulIslam , i'm not getting you. What should i do for my code?
– Bikash Adhikari
Nov 11 at 8:10












check my answer, you have to add onRequestPermissionsResult callback
– Touhidul Islam
Nov 11 at 8:18




check my answer, you have to add onRequestPermissionsResult callback
– Touhidul Islam
Nov 11 at 8:18












1 Answer
1






active

oldest

votes


















1














When your permission is given/denied, there is a callback method called, named onRequestPermissionsResult(int requestCode, String permissions, int grantResults).



The callback returns with the requestCode which was the code permission was called with first.



All you have to check




  1. The requestCode equals to what permission you asked for


  2. grandResult array is not empty


  3. grantResults[0] is equal to PackageManager.PERMISSION_GRANTED



Example:



@Override
public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults) {

switch (requestCode) {
case MY_PERMISSIONS_REQUEST: { // here, MY_PERMISSIONS_REQUEST is the intended request code
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

// permission was granted, yay! Do the 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%2f53246319%2fapp-doesnt-respond-for-camera-permission%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    When your permission is given/denied, there is a callback method called, named onRequestPermissionsResult(int requestCode, String permissions, int grantResults).



    The callback returns with the requestCode which was the code permission was called with first.



    All you have to check




    1. The requestCode equals to what permission you asked for


    2. grandResult array is not empty


    3. grantResults[0] is equal to PackageManager.PERMISSION_GRANTED



    Example:



    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults) {

    switch (requestCode) {
    case MY_PERMISSIONS_REQUEST: { // here, MY_PERMISSIONS_REQUEST is the intended request code
    // If request is cancelled, the result arrays are empty.
    if (grantResults.length > 0
    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

    // permission was granted, yay! Do the 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


























      1














      When your permission is given/denied, there is a callback method called, named onRequestPermissionsResult(int requestCode, String permissions, int grantResults).



      The callback returns with the requestCode which was the code permission was called with first.



      All you have to check




      1. The requestCode equals to what permission you asked for


      2. grandResult array is not empty


      3. grantResults[0] is equal to PackageManager.PERMISSION_GRANTED



      Example:



      @Override
      public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults) {

      switch (requestCode) {
      case MY_PERMISSIONS_REQUEST: { // here, MY_PERMISSIONS_REQUEST is the intended request code
      // If request is cancelled, the result arrays are empty.
      if (grantResults.length > 0
      && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

      // permission was granted, yay! Do the 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
























        1












        1








        1






        When your permission is given/denied, there is a callback method called, named onRequestPermissionsResult(int requestCode, String permissions, int grantResults).



        The callback returns with the requestCode which was the code permission was called with first.



        All you have to check




        1. The requestCode equals to what permission you asked for


        2. grandResult array is not empty


        3. grantResults[0] is equal to PackageManager.PERMISSION_GRANTED



        Example:



        @Override
        public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults) {

        switch (requestCode) {
        case MY_PERMISSIONS_REQUEST: { // here, MY_PERMISSIONS_REQUEST is the intended request code
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        // permission was granted, yay! Do the 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












        When your permission is given/denied, there is a callback method called, named onRequestPermissionsResult(int requestCode, String permissions, int grantResults).



        The callback returns with the requestCode which was the code permission was called with first.



        All you have to check




        1. The requestCode equals to what permission you asked for


        2. grandResult array is not empty


        3. grantResults[0] is equal to PackageManager.PERMISSION_GRANTED



        Example:



        @Override
        public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults) {

        switch (requestCode) {
        case MY_PERMISSIONS_REQUEST: { // here, MY_PERMISSIONS_REQUEST is the intended request code
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        // permission was granted, yay! Do the 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 Nov 11 at 6:25









        Touhidul Islam

        1,0211314




        1,0211314






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53246319%2fapp-doesnt-respond-for-camera-permission%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini