I have a project in which I request 2 permissions at startup. When debugging, it works as intended until it makes a second call to requestPermissions . As soon as this happens, the first runs onRequestPermissionsResult with an empty grantResults array.
It has worked to this day. The only thing I can come up with is the updates I made today. I upgraded to the Android SDK Platform Tools 23.1 and upgraded my Mac to El Capitan.
Can someone help me fix everything that was?
Code follows:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.blank_layout); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { int hasCameraPermissions = checkSelfPermission(android.Manifest.permission.CAMERA); if (hasCameraPermissions != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{android.Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION_CODE); } int hasStoragePermissions = checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE); if (hasStoragePermissions != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE_PERMISSION_CODE); } if (hasCameraPermissions == PackageManager.PERMISSION_GRANTED && hasStoragePermissions == PackageManager.PERMISSION_GRANTED) { startCameraIntent(); } } else { startCameraIntent(); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case REQUEST_CAMERA_PERMISSION_CODE: if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, R.string.camera_denied, Toast.LENGTH_LONG).show(); quitAfter5(); } break; case REQUEST_STORAGE_PERMISSION_CODE: if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, R.string.storage_denied, Toast.LENGTH_LONG).show(); quitAfter5(); } break; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { int hasCameraPermissions = checkSelfPermission(android.Manifest.permission.CAMERA); int hasStoragePermissions = checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE); if (hasCameraPermissions == PackageManager.PERMISSION_GRANTED && hasStoragePermissions == PackageManager.PERMISSION_GRANTED) { startCameraIntent(); } } }
EDIT :: The change that caused this crash was the fact that I installed the ElementalX user kernel on my Nexus 5 a few days earlier. I confirmed this question only yesterday on my Nexus 6 brother using the same core. I am going to reveal a letter to the kernel developer to find out what he says about it.
As in the answer, the code above is really the wrong way to use the requestPermissions method anyway. So everyone just does it right and will work on all devices :)
source share