2

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>
  • You should look [this post](https://stackoverflow.com/questions/34342816/android-6-0-multiple-permissions) and James McCracken's answer – Beyazid Mar 12 '19 at 18:00
  • What android version are you using? As of API 23 you must ask for permissions at runtime. – Ian Rehwinkel Mar 12 '19 at 18:39
  • @Beyazidy Actually im maming a project on AR which has a arcamera which asks for camera feature in the starting . (from the manifest file) . so i just need WRITE_EXTERNAL_Storage to also ask after that . but can't – Ishaan Gupta Mar 12 '19 at 18:43
  • @IanRehwinkel yes i am asking at runtime . but first my apk checks for camera permission at the start of the ap then it crashes if i try to use storage. then on opening again it asks for storage permission which i did through code. – Ishaan Gupta Mar 12 '19 at 18:44
  • Did you try asking camera permission in your code instead of manifest file? – Beyazid Mar 12 '19 at 18:46
  • You can add multiple permission requests in a single dialog like so: `ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_CONTACTS /* more permissions separated with comma*/ }, MY_PERMISSIONS_REQUEST_READ_CONTACTS);` (taken from here: https://developer.android.com/training/permissions/requesting – Ian Rehwinkel Mar 12 '19 at 18:47
  • check the manifest. i edited my post. camera permission is there itself even if i dont write code for it – Ishaan Gupta Mar 12 '19 at 18:51

1 Answers1

0

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.

Amol Gangadhare
  • 1,059
  • 2
  • 11
  • 24