I know this question has been asked number of times but none of the methods are working for me. I have a v4.nested fragment and I am calling requestPermission but I don't even get the dialog to show and hence onRequestPermissionsResult is not getting called. I have tried call the onRequestPermissionsResult in both my Activity and Fragment but nothing is working. I am using appcompat-v7:23.3.0 so I was expecting the dialog and hence the result. Any help? Thanks
Code
public void ask(){
Log.e("Here", "Permission");
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_GRANTED) {
Log.e("Here", "Granted");
if (what == 1){
PackageManager pm = getActivity().getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
getActivity().startActivityForResult(i, CAMERA_PIC_REQUEST);
} else {
Toast.makeText(getActivity(), "Can't detect Camera on device", Toast.LENGTH_LONG).show();
}
}
else{
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
getActivity().startActivityForResult(i, RESULT_LOAD_IMAGE);
}
}
else{
Log.e("Here", "Ask");
if (useRuntimePermissions()) {
if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
showMessageOKCancel("You need to allow access to Device Storage",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 3);
Log.e("A", "ash");
}
});
} else {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},2);
Log.e("B", "Should be Asked by now");
}
}
}
}
private boolean useRuntimePermissions() {
return(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new android.support.v7.app.AlertDialog.Builder(getActivity())
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
Log.e("Hehe", "results"+String.valueOf(requestCode));
switch (requestCode) {
case CAM_REQUEST: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (what == 1){
PackageManager pm = getActivity().getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
getActivity().startActivityForResult(i, CAMERA_PIC_REQUEST);
} else {
Toast.makeText(getActivity(), "Can't detect Camera on device", Toast.LENGTH_LONG).show();
}
}
else{
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
getActivity().startActivityForResult(i, RESULT_LOAD_IMAGE);
}
}
else {
Toast.makeText(getActivity(), "Camera Request not granted. Please enable it at App Settings", Toast.LENGTH_LONG).show();
}
return;
}
}
}
The ask() method is called from a button tap onsetClickListener and LogA and LogB are logging but the permission dialog doesn't show.