I am to new to Android 6.0 Coding Please Provide a solutions For the Below Code:
When i provide Run Time Permissions like READ_EXTERNAL_STORAGE
and WRITE_EXTERNAL_STORAGE
it shows an Exception like
java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.motorola.camera/.Camera clip={text/uri-list U:file:///storage/emulated/0/Pictures/MyAppNew%20File%20Upload/IMG_20160401_110234.jpg} (has extras) } from ProcessRecord{ed96564 26955:com.social.nocializer/u0a259} (pid=26955, uid=10259) with revoked permission android.permission.CAMERA
Either MediaStore.ACTION_IMAGE_CAPTURE
and MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE
Run Time Permissions are not working...
Note: READ_EXTERNAL_STORAGE
Works For When Opening Gallery
You have to manage run time permission for this, Because whichever permissions you have defined in AndroidManifest will not be automatically granted. So like below method you can check whether you permission is approved or not
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
MY_REQUEST_CODE);
}
Here, MY_REQUEST_CODE is a static constant that you can define, which will be used again for the requestPermission dialog callback. Now, You will need a callback for the dialog result:
@Override
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == MY_REQUEST__CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Now user should be able to use camera
}
else {
// Your app will not have this permission. Turn off all functions
// that require this permission or it will force close like your
// original question
}
}
}
@Ronak Solution worked for me but with a few following changes, As we need to check on only those devices which are above Android M.
if( ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.CAMERA},
5);
}
}
And override the following method using crl+o copying, pasting would might result in an error :D
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 5) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Now user should be able to use camera
}
else {
// Your app will not have this permission. Turn off all functions
// that require this permission or it will force close like your
// original question
}
}