1

I am trying to use the "new" way of requesting external storage write permission. But the request is automatically denied and no window pops up asking the user for permission.

Virtual Device: Pixel 2 API 30 SDK 30

I did add the permissions into the manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lab6">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE "/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"

WRITE_EXTERNAL_STORAGE is a dangerous permission from my knowledge, so it has to be requested at runtime.

into my gradle dependencies I added these:

implementation 'androidx.activity:activity:1.2.0'
implementation 'androidx.fragment:fragment:1.3.0'

The activity looks like this:

public class MainActivity extends AppCompatActivity {

    private ActivityResultLauncher<String> requestPermissionLauncher =
            registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
                if (isGranted) {
                    Log.d("mihai", "Permission is granted now.");
                } else {
                    Log.d("mihai", "Permission refused ");
                }
            });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button downloadBtn = findViewById(R.id.downloadButton);
        Button loadBtn = findViewById(R.id.loadButton);

        downloadBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("mihai", "requesting permission");
                if (ContextCompat.checkSelfPermission(
                        getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
                        PackageManager.PERMISSION_GRANTED) {

                    Log.d("mihai", "permission already granted");
                } else {
                    Log.d("mihai", "launching");
                    requestPermissionLauncher.launch(
                            Manifest.permission.WRITE_EXTERNAL_STORAGE);

                }
            }
        });
    }
}

And these are the logs I get. As you can see, the request is denied. No question pops up on the phone.

2022-04-28 19:55:15.351 7499-7499/com.example.lab6 D/mihai: requesting permission
2022-04-28 19:55:15.353 7499-7499/com.example.lab6 D/mihai: launching
2022-04-28 19:55:15.592 7499-7499/com.example.lab6 D/mihai: Permission refused 
Mihai
  • 100
  • 13

1 Answers1

0

Because you have denied the permission multiple times.

From the documentation:

Starting in Android 11 (API level 30), if the user taps Deny for a specific permission more than once during your app's lifetime of installation on a device, the user doesn't see the system permissions dialog if your app requests that permission again. The user's action implies "don't ask again." On previous versions, users would see the system permissions dialog each time your app requested a permission, unless the user had previously selected a "don't ask again" checkbox or option.

You will need to redirect the user to the app's setting detail to grant the permission manually.

To open an app specific app's detail setting screen:

Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
Sovathna Hong
  • 404
  • 4
  • 5