Saturday, 9 July 2016

Runtime Permissions in android Marshmallow

Hi guys, I am back with a very important topic necessary for every android application called Run-time permission in Marshmallow. As we know that Google changed permission management in Marshmallow. So if you want your app in Marshmallow you need to handle all permission at run-time. I will not write more talking stuff so Lets start with an example of capturing an image.

Open your layout (activity_main.xml) and  add two image to it one for captured image and one for camera click.
Open your java file (MainActivity.java) and Register both imageview and add setOnClickListener to camera click imageview.
Inside onClick of camera imageview call  checkCameraPermission() method ,if permission is granted then launch camera else call  requestCameraPermission() method to open permission dialog.


Create method to launch camera.

/**
     * Method to launch camera after permission accepted from user
     */
    void takePicture() {
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        startActivityForResult(intent, 0);
    }


Create a method to check permission for camera as follows:

void checkCameraPermission() {
        boolean isGranted;
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            // Camera permission has not been granted.
                requestCameraPermission();
} else {
               takePicture();
        }
    }


Implement ActivityCompat.OnRequestPermissionsResultCallback in MainActivity as:

public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback



Override onRequestPermissionResult() method.
Inside onRequestPermissionResult() check permission, if it is granted then launch camera else show a Toast.

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CAMERA) {
            // BEGIN_INCLUDE(permission_result)
            // Received permission result for camera permission.
            Log.i(TAG, "Received response for Camera permission request.");

            // Check if the only required permission has been granted
            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // Camera permission has been granted, preview can be displayed

                takePicture();

            } else {
              //Permission not granted
                Toast.makeText(MainActivity.this,"You need to grant camera permission to use camera",Toast.LENGTH_LONG).show();
            }

        }
    }



Override onActivityResult() method to get image from camera and set it to imageview.

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            if (resultCode == Activity.RESULT_OK && requestCode == 0) {

            //this is your bitmap image and now you can do whatever you want with this

            Bitmap picture = (Bitmap) data.getExtras().get("data");

            iv_pic.setImageBitmap(picture);
         }
    }

That's all. and if still have any problem feel free to comment below.
Happy coding and remember, Code to make it better!!




No comments:

Post a Comment