Skip to content

Handle Scoped Storage

On Android 11 and later, Scoped Storage is stricter and limits access to shared storage, which can affect file I/O and sample path access. This section describes common impact points and optional mitigations for integration and troubleshooting.

1. Disable Scoped Storage (Optional)

You can disable Scoped Storage in either of these ways:

  • In the app build.gradle, set targetSdkVersion to <= 28.
  • Set targetSdkVersion to 29 and add the following on the application node in AndroidManifest.xml:
xml
android:requestLegacyExternalStorage="true"

2. When targetSdkVersion >= 30

If targetSdkVersion >= 30, use the configuration below to reduce Scoped Storage impact.

In AndroidManifest.xml:

  1. Add to the manifest node:
xml
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
  1. Add to the application node:
xml
android:requestLegacyExternalStorage="true"
android:preserveLegacyExternalStorage="true"

3. Runtime Authorization (Sample Code)

In MainActivity.java, add the following as needed to request runtime permissions:

java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    if (!Environment.isExternalStorageManager()) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
        intent.setData(Uri.parse("package:" + getApplicationContext().getPackageName()));
        startActivityForResult(intent, REQUEST_ALL_FILES_ACCESS_PERMISSION);
        return;
    }
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    int permission = ContextCompat.checkSelfPermission(this.getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permission != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
        return;
    }
}

To adapt with Scoped Storage enabled, see permission and file access logic in MainActivity.java in the full reader (UI Extensions) sample.

See also MainActivity.java in the basic reader (PDFViewCtrl) sample.