Monday, 16 May 2016

Runtime permissions android m example | Runtime permission in android marshmallow

Helllllooooooooo warriors finally after a long time I am back with 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.



In this post i am going to make a sample app to capture image from camera and set in Imageview. for that we need camera permission and I am going handle that permission at run-time. So like every post I will make a module of steps and then we will follow that to implement.

  1. Create a new project in Android studio, File>>New>>New project.
  2. Open Manifest file and add camera permission to it.
  3. Open activity_main.xml and two image to it one for captured image and one for camera click.
  4. Open MainActivity.java and Register both imageview and add setOnClickListener to camera click imageview.
  5. Create a method to check permission for camera.
  6. Implement ActivityCompat.OnRequestPermissionsResultCallback in MainActivity.
  7. Override onRequestPermissionResult() method.
  8. Create method to launch camera.
  9. Override onActivityResult() method to get image from camera and set it to imageview.
  10. Inside onClick of camera imageview call  checkCameraPermission() method ,if permission is granted then launch camera else call  requestCameraPermission() method to open permission dialog.
  11. Inside onRequestPermissionResult() check permission, if it is granted then launch camera else show a Toast.
Now we will follow each stop listed above to complete this tutorial.

1.Create a new project in Android studio, File>>New>>New project  like below image.


 2.Open Manifest file and add camera permission to it ,simply open your manifest file and add below code inside application tag.


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

like below image.


3.Open activity_main.xml and two image to it one for captured image and one for camera click. here is my activity_main.xml.

4.Open MainActivity.java and Register both imageview and add setOnClickListener to camera click imageview, like below code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
 private ImageView iv_camera, iv_pic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        iv_pic = (ImageView) findViewById(R.id.iv_pic);

        iv_camera = (ImageView) findViewById(R.id.iv_camera);

        iv_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
            }
        });

    }


5.Create a method to check permission for camera like below code .

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
 /**
     * Method to check permission
     */
    void checkCameraPermission() {
        boolean isGranted;
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            // Camera permission has not been granted.

            requestCameraPermission();


        } else {

            takePicture();

        }
    }

In above code takePicture() method is for launching camera using intent and code is here.

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

6.Implement ActivityCompat.OnRequestPermissionsResultCallback in MainActivity like below code.
1
public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback 

7.Override onRequestPermissionResult() method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
 @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();
            }

        }
    }

in above method I am checking for request used at time of requesting permission , if code match and permission is granted then call takePicture() method to launch camera else show a message using toast.

8.Override onActivityResult() method to get image from camera and set it to imageview like below code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
@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);


        }
    }

in above method i am taking image as bitmap and setting it to imageview.

Now here is my complete MainActivity.java class.
you can download sample app from below download button.

https://codeload.github.com/kundansviet/Runtime-permission-in-Marshmallow/zip/master

Still Having any problem feel free to comment below.

0 comments:

Post a Comment