Some users in my app get this error:
Caused by: java.lang.ArrayIndexOutOfBoundsException: at
com.file.gt.MainActivity.onRequestPermissionsResult
(MainActivity.java:234) at android.app.Activity.requestPermissions
(Activity.java:5323) at com.file.gt.MainActivity.onCreate
(MainActivity.java:85) at android.app.Activity.performCreate
(Activity.java:8114) at android.app.Activity.performCreate
(Activity.java:8098) at
android.app.Instrumentation.callActivityOnCreate
(Instrumentation.java:1309) at
android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:3480)
It occurs only sometimes, with some users.
I have this in my app:
private final String [] permissions = {
"android.permission.RECORD_AUDIO",
"android.permission.READ_PHONE_STATE"
};
int requestCode = 200;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(permissions[0]) == PackageManager.PERMISSION_DENIED ||
checkSelfPermission(permissions[1]) == PackageManager.PERMISSION_DENIED) {
requestPermissions(permissions, requestCode);
}
}
and this:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
recreate(); // 234
}
else {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle(getString(R.string.alert_title));
alertDialog.setMessage(getString(R.string.alert_text));
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
(dialog, which) -> dialog.dismiss());
alertDialog.show();
}
}
Any ideas what is wrong and what I'm doing wrong?
Thanks.
When you get an ArrayIndexOutOfBoundsException it means you are accessing an array index outside the array size. Your stack trace says it happens in onRequestPermissionsResult, and the only array you access there is grantResults.
What can sometimes happen is that grantResults is empty in onRequestPermissionsResult if the user declines the permissions, so you should check its size before accessing its members
if(grantResults.length == 2 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED &&
grantResults[1] == PackageManager.PERMISSION_GRANTED) {
recreate();
}
else {
// handle permissions not granted
}
Alternately, if you use the new registerForActivityResult approach you can get around this entirely. Have a look here for more info, or here for an example use.
private final String [] permissions = {
android.Manifest.permission.RECORD_AUDIO,
android.Manifest.permission. READ_PHONE_STATE,
};
int requestCode = 200;
public static boolean hasPermissions(Context context, String... permissions) {
if (context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;}
//user this code to check
if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}else{
recreate();
}
Related
So I used a tutorial and pretty much copied and only changed a little if the user grants permission the app works after the user leaves the app and comes back. If they open the app and grant permission nothing happens, also if they deny permission nothing happens.
public class MainActivity extends AppCompatActivity {
private ArrayList<Weather> weatherLsit;
public static int REQUEST_PERMISSION=1;
private RecyclerView recyclerView;
private EditText zipCodeEditText;
LocationDataService locationDataService = new LocationDataService(MainActivity.this, this);
FusedLocationProviderClient fusedLocationProviderClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.hourly_recycle_view);
weatherLsit = new ArrayList<>();
weatherLsit.add(new Weather(10, 6, R.drawable.cloudy_icon));
weatherLsit.add(new Weather(15, 7, R.drawable.cloudy_icon));
weatherLsit.add(new Weather(10, 8, R.drawable.cloudy_icon));
weatherLsit.add(new Weather(60, 9, R.drawable.cloudy_icon));
weatherLsit.add(new Weather(80, 10, R.drawable.cloudy_icon));
weatherLsit.add(new Weather(20, 12, R.drawable.cloudy_icon));
weatherLsit.add(new Weather(13, 12, R.drawable.cloudy_icon));
setAdapter();
}
#Override
protected void onStart() {
super.onStart();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationDataService.getLastLocation();
} else {
locationDataService.requestLocationPermission();
}
}
private void setAdapter() {
WeatherAdapter adapter = new WeatherAdapter(weatherLsit);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION) {
if (permissions.length >=0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
System.out.println("PERMISSON HAS BEEN GRANTED!");
//do something permission is allowed here....
} else {
Toast.makeText(this, "Please allow the Permission", Toast.LENGTH_SHORT).show();
}
}
}
}
for some reason, the onRequestPermissionsResult isn't being executed I've spent a couple of hours already on this problem, and for some reason, I cant get it to work.
You can try putting the permission check in the onCreate() method.
like following:
And change the permission code to the following:
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
Log.i(TAG, "sdk < 28 Q");
if (ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
String[] strings =
{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
ActivityCompat.requestPermissions((Activity)context, strings, 1);
}
} else {
if (ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context,
"android.permission.ACCESS_BACKGROUND_LOCATION") != PackageManager.PERMISSION_GRANTED) {
String[] strings = {Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
"android.permission.ACCESS_BACKGROUND_LOCATION"};
ActivityCompat.requestPermissions((Activity)context, strings, 2);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults.length > 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "onRequestPermissionsResult: apply LOCATION PERMISSION successful");
} else {
Log.i(TAG, "onRequestPermissionsResult: apply LOCATION PERMISSSION failed");
}
}
if (requestCode == 2) {
if (grantResults.length > 2 && grantResults[2] == PackageManager.PERMISSION_GRANTED
&& grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "onRequestPermissionsResult: apply ACCESS_BACKGROUND_LOCATION successful");
} else {
Log.i(TAG, "onRequestPermissionsResult: apply ACCESS_BACKGROUND_LOCATION failed");
}
}
}
I am trying to request 2 permissions when the app opens up. It works fine, but when the user selects never ask again, a toast message should be displayed once. The toast message keeps on coming up repeatedly and does not stop. I cannot figure out the reason for this.
public void checkPermission(final Context context) {
if(ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.CAMERA)) {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
}
else {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Both permissions granted", Toast.LENGTH_SHORT).show();
} else {
checkPermission(this);
}
if (requestCode == 1) {
for (int i = 0, len = permissions.length; i < len; i++) {
String permission = permissions[i];
boolean showRationale;
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
showRationale = shouldShowRequestPermissionRationale(permission);
if (!showRationale) {
Toast.makeText(this, "Denied", Toast.LENGTH_SHORT).show();
}
}
}
}
}
Above is the code that checks for permissions.
I would like to know why the toast message never stops popping up.
Try following code -
public class MainActivity extends AppCompatActivity{
private static final int STORAGE_PERMISSION_REQ_CODE = 1001;
private static final int CAMERA_PERMISSION_REQ_CODE = 1002;
private static final int BOTH_PERMISSION_REQ_CODE = 1003;
...........
private void checkPermission(){
if(hasBothPermission()){
// Has both permission
}
else if(hasReadStoragePermission(READ_EXTERNAL_STORAGE))
// Has Storage Permission request Camera Permission
requestPermissions(new String[]{CAMERA}, CAMERA_PERMISSION_REQ_CODE);
else if(hasCameraPermission(CAMERA))
// Has Camera Permission request Storage Permission
requestPermissions(new String[]{READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_REQ_CODE);
else
// Request Both Permission
requestPermissions(new String[]{READ_EXTERNAL_STORAGE, CAMERA},BOTH_PERMISSION_REQ_CODE);
}
private boolean hasBothPermission(){
return (ContextCompat.checkSelfPermission(this, CAMERA) == PackageManager.PERMISSION_GRANTED) &&
ContextCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}
private boolean hasCameraPermission(String perm) {
return (ContextCompat.checkSelfPermission(this, perm) == PackageManager.PERMISSION_GRANTED);
}
private boolean hasReadStoragePermission(String perm) {
return (ContextCompat.checkSelfPermission(this, perm) == PackageManager.PERMISSION_GRANTED);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if(requestCode == STORAGE_PERMISSION_REQ_CODE){
if(hasReadStoragePermission(READ_EXTERNAL_STORAGE))
Toast.makeText(this, "Storage Permission Granted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Storage Permission Failed, Request Again", Toast.LENGTH_SHORT).show();
}
else if(requestCode == CAMERA_PERMISSION_REQ_CODE){
if(hasCameraPermission(CAMERA))
Toast.makeText(this, "Camera Permission Granted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Camera Permission Failed, Request Again", Toast.LENGTH_SHORT).show();
}
else {
if(hasBothPermission())
Toast.makeText(this, "Permission Granted Success", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Permission Failed Request Permission Again", Toast.LENGTH_SHORT).show();
}
}
...........
}
Some time ago i added permission to my app about 'fine location' by some tutorial from internet. Everything works like i want, but i have question about 1 unexplained line:
private static final int MY_PERMISSION_REQUEST_FINE_LOCATION = 101;
Why is there value 101 ? Why not 102 or 200 ?
private static final int MY_PERMISSION_REQUEST_FINE_LOCATION = 101;
.
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSION_REQUEST_FINE_LOCATION);
} else {
}
return;
}
.
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSION_REQUEST_FINE_LOCATION:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
LatLng position = new LatLng(0, 0);
} else {
close();
}
}
}
You can give it the value you want.It's worth doing the job onRequestPermissionResult to catch the requestCode
In the documentation of requestPermissions(), it says that the requestCode is for
Application specific request code to match with a result reported to
onRequestPermissionsResult(int, String[], int[]). Should be >= 0.
What it means is that you'll explicitly specify this code to any values that you want, and you can then use this value to check if your request is successful or not in the callback onRequestPermissionsResult. An example, put this code in your Activity:
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 101:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//granted
} else {
//not granted
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
So back to your question, you can use 102, or 200, or whatever value you would like. But you should always check if the request is successful or not, by using the same code that you provide in the requestPermissions method.
im trying to show multiple permission request on Android 7.0
This code working with no problem on Android sdk 23.
SDK 24 showing one time the request, if i touch allow or denied button, this code working one time and Don't show other request.
Code is here;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
if (checkSelfPermission(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_DENIED) {
requestPermissions(new String[]{Manifest.permission.READ_SMS}, 1);
}
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}
}
try this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_SMS, Manifest.permission.ACCESS_FINE_LOCATION};
boolean flag = false;
for (int i = 0; i < permissions.length; i++) {
if (checkSelfPermission(permissions[i]) == PackageManager.PERMISSION_DENIED) {
flag = true;
break;
}
}
if (flag) {
requestPermissions(permissions, 1);
}
}
First of all check if permission is already granted and if not granted then ask for permission using below code.
String[] PERMISSIONS = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_CALL_LOG, Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_CONTACTS};
if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
Once permissions are granted, you can execute below code to check whether a particular permission was granted or denied
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_ALL: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
}
if (grantResults[1] == PackageManager.PERMISSION_GRANTED) {
} else {
}
if (grantResults[2] == PackageManager.PERMISSION_GRANTED) {
} else {
}
if (grantResults[3] == PackageManager.PERMISSION_GRANTED) {
} else {
}
if (grantResults[4] == PackageManager.PERMISSION_GRANTED) {
} else {
}
} else {
//All permission denied
}
runAfterPermissionGranted();
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
i want to ask the user to accept the following permissions at the same time(one by one), the permissions are like:
checkLocationPermission, checkReadSMS, checkCallingPermission, checkReadState, checkContactWriteState.
So, how i can ask all these permissions in my first screen itself.
Please help me out in this regard.
Thanks in advance.
You have to first check that user phone build version is 23.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
askPermissions(true);
} else {
startActivity(new Intent(PermissionsActivity.this, SplashActivity.class));
finish();
}
If version is 23 then you need to ask permissions.
private void askPermissions(boolean isForOpen) {
isRationale = false;
List permissionsRequired = new ArrayList();
final List<String> permissionsList = new ArrayList<String>();
if (!checkPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE))
permissionsRequired.add("Write External Storage");
if (!checkPermission(permissionsList, Manifest.permission.CALL_PHONE))
permissionsRequired.add("Call phone");
if (!checkPermission(permissionsList, Manifest.permission.READ_PHONE_STATE))
permissionsRequired.add("Read phone state");
if (!checkPermission(permissionsList, Manifest.permission.READ_CONTACTS))
permissionsRequired.add("Read Contacts");
if (!checkPermission(permissionsList, Manifest.permission.RECEIVE_SMS))
permissionsRequired.add("Receive SMS");
if (!checkPermission(permissionsList, Manifest.permission.GET_ACCOUNTS))
permissionsRequired.add("Get Accounts");
if (!checkPermission(permissionsList, Manifest.permission.ACCESS_COARSE_LOCATION))
permissionsRequired.add("Location");
if (!checkPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION))
permissionsRequired.add("Location");
if (permissionsList.size() > 0 && !isRationale) {
if (permissionsRequired.size() > 0) {
}
if (isForOpen) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ActivityCompat.requestPermissions(this, permissionsList.toArray(new String[permissionsList.size()]),
11);
}
}
} else if (isRationale) {
if (isForOpen) {
new android.support.v7.app.AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle)
.setTitle("Permission Alert")
.setMessage("You need to grant permissions manually. Go to permission and grant all permissions.")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, 123);
}
})
.show();
}
} else {
startActivity(new Intent(PermissionsActivity.this, SplashActivity.class));
finish();
}
}
private boolean checkPermission(List permissionsList, String permission) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permission);
// Check for Rationale Option
if (!isFirst) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
isRationale = true;
return false;
}
}
}
}
return true;
}
on the onRequestPermissionsResult you need to check which permissions granted
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case 11:
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.CALL_PHONE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_PHONE_STATE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_CONTACTS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.RECEIVE_SMS, PackageManager.PERMISSION_GRANTED);
// Fill with results
for (int i = 0; i < permissions.length; i++) {
perms.put(permissions[i], grantResults[i]);
}
// Check for ACCESS_FINE_LOCATION
if (perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED &&
perms.get(Manifest.permission.RECEIVE_SMS) == PackageManager.PERMISSION_GRANTED) {
// All Permissions Granted
startActivity(new Intent(PermissionsActivity.this, SplashActivity.class));
finish();
} else {
// Permission Denied
Toast.makeText(this, "Some Permission is Denied.", Toast.LENGTH_SHORT)
.show();
isFirst = false;
askPermissions(true);
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
If user has set the permission to never ask again then the application setting screen will open. User will allow/Deny permission there. You need to check again on the activityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
askPermissions(true);
}
if (!hasPermissions()){
// your app doesn't have permissions, ask for them.
requestNecessaryPermissions();
}
else {
// your app already have permissions allowed.
// do what you want.
}
private boolean hasPermissions() {
int res = 0;
// list all permissions which you want to check are granted or not.
String[] permissions = new String[] {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
for (String perms : permissions){
res = checkCallingOrSelfPermission(perms);
if (!(res == PackageManager.PERMISSION_GRANTED)){
// it return false because your app dosen't have permissions.
return false;
}
}
// it return true, your app has permissions.
return true;
}
private void requestNecessaryPermissions() {
// make array of permissions which you want to ask from user.
String[] permissions = new String[] {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// have arry for permissions to requestPermissions method.
// and also send unique Request code.
requestPermissions(permissions, REQUEST_CODE_STORAGE_PERMS);
}
}
/* when user grant or deny permission then your app will check in
onRequestPermissionsReqult about user's response. */
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grandResults) {
// this boolean will tell us that user granted permission or not.
boolean allowed = true;
switch (requestCode) {
case REQUEST_CODE_STORAGE_PERMS:
for (int res : grandResults) {
// if user granted all required permissions then 'allowed' will return true.
allowed = allowed && (res == PackageManager.PERMISSION_GRANTED);
}
break;
default:
// if user denied then 'allowed' return false.
allowed = false;
break;
}
if (allowed) {
// if user granted permissions then do your work.
dispatchTakePictureIntent();
}
else {
// else give any custom waring message.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
Toast.makeText(MainActivity.this, "Camera Permissions denied", Toast.LENGTH_SHORT).show();
}
else if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
Toast.makeText(MainActivity.this, "Storage Permissions denied", Toast.LENGTH_SHORT).show();
}
}
}
}
follow this tutorial, in this tutorial multiples permissions are asked
Android 6.0 Runtime Permissions
the Kotlin version
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.fragment.app.Fragment
class MyFragment : Fragment() {
companion object {
val TAG: String = MyFragment::class.java.simpleName
var PERMISSIONS = arrayOf(
Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
}
private val permReqLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
val granted = permissions.entries.all {
it.value == true
}
if (granted) {
displayCameraFragment()
}
}
private fun takePhoto() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
displayCameraFragment()
}
activity?.let {
if (hasPermissions(activity as Context, PERMISSIONS)) {
displayCameraFragment()
} else {
permReqLauncher.launch(
PERMISSIONS
)
}
}
}
// util method
private fun hasPermissions(context: Context, permissions: Array<String>): Boolean = permissions.all {
ActivityCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
}
private fun displayCameraFragment() {
// open camera fragment
}
}