I have added permission for camera in my manifest file. So whenever my apk is built it asks for camera permission. Then after that I added a code for runtime write_storage_permission but when I build app it first ask for camera permission then if I do something that requires storage permission app crashes and when I open app again it then ask for permission.
So how can I set that whenever my app is built it asks for camera permission (from manifest file) then just after that asks for write_external_storage permission.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.download">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data android:name="com.google.ar.core" android:value="required" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
First, check the permissions are granted which you will need.
int rc1 = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
int rc2 = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int rc3 = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
int rc4 = ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET);
if (rc1 == PackageManager.PERMISSION_GRANTED &&
rc2 == PackageManager.PERMISSION_GRANTED &&
rc3 == PackageManager.PERMISSION_GRANTED &&
rc4 == PackageManager.PERMISSION_GRANTED) {
allGoodProceed();
} else {
requestPermission();
}
then call requestPermission
method.
private void requestPermission() {
final String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA,
Manifest.permission.INTERNET};
boolean isPermissionDeniedPermanent = false;
for (int i = 0, len = permissions.length; i < len; i++) {
String permission = permissions[i];
if (ActivityCompat.checkSelfPermission(this, permissions[i]) == PackageManager.PERMISSION_DENIED) {
// user rejected the permission
boolean showRationale = shouldShowRequestPermissionRationale(permission);
if (showRationale) {
isPermissionDeniedPermanent = showRationale;
}
}
}
if (isPermissionDeniedPermanent) {
showPermissionsDialog();
} else {
if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
ActivityCompat.requestPermissions(this, permissions, HANDLE_STORAGE_PERMISSION);
}
}
}
Also, override onRequestPermissionsResult
method to check permissions result.
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
boolean isSomePermissionsMissing = false;
for (int i = 0, len = permissions.length; i < len; i++) {
if (ActivityCompat.checkSelfPermission(this, permissions[i]) == PackageManager.PERMISSION_DENIED) {
isSomePermissionsMissing = true;
}
}
if (isSomePermissionsMissing) {
showPermissionsDialog();
} else {
//all good proceed...
}
}
private void showPermissionsDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Action required").setMessage("To proceed you have to grant some permissions");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alertDialog=builder.create();
alertDialog.show();
}
You can add the permissions to be checked before proceeding as you need. Please revert back if this resolves your query.