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, settargetSdkVersionto<= 28. - Set
targetSdkVersionto29and add the following on theapplicationnode inAndroidManifest.xml:
xml
android:requestLegacyExternalStorage="true"2. When targetSdkVersion >= 30
If targetSdkVersion >= 30, use the configuration below to reduce Scoped Storage impact.
In AndroidManifest.xml:
- Add to the
manifestnode:
xml
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />- Add to the
applicationnode:
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.