Reputation: 679
I'm trying to get the MANAGE_EXTERNAL_STORAGE permission on my Android 10 (API 29) device.
https://developer.android.com/training/data-storage/manage-all-files
Manifest:
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
MainActivity:
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
startActivity(intent);
The result is:
No Activity found to handle Intent { act=android.settings.MANAGE_APP_ALL_FILES_ACCESS_PERMISSION }
Okay, this behaviour is possible. The doc says:
In some cases, a matching Activity may not exist, so ensure you safeguard against this.
But how can I obtain that permission?
Upvotes: 26
Views: 46942
Reputation: 1
// Define the permissions variable correctly
private val storagePermissions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
arrayOf(
Manifest.permission.READ_MEDIA_IMAGES // Android 13+ (API 33) specific permission
)
} else {
arrayOf(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
}
private fun checkAndRequestPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { // Android 11+ (API 30)
// Check if we have "All Files Access" permission (MANAGE_EXTERNAL_STORAGE)
if (Environment.isExternalStorageManager()) {
readDataFromCSV()// Permission granted, proceed with saving data
} else {
try {
// Only request permission if supported by the device
requestAllFilesAccessPermission()
} catch (e: Exception) {
Log.e("MainActivity", "Error requesting permission: ${e.message}")
Toast.makeText(this, "Unable to request permission. Please enable manually in settings.", Toast.LENGTH_LONG).show()
}
}
} else {
// For Android versions below 11 (API < 30), request legacy permissions
if (!hasStoragePermissions()) {
ActivityCompat.requestPermissions(this, storagePermissions, REQUEST_CODE_STORAGE)
} else {
readDataFromCSV() // Permission granted, proceed with saving data
}
}
}
// Request "All Files Access" permission for Android 11+ (API 30+)
private fun requestAllFilesAccessPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager()) {
//todo when permission is granted
} else {
//request for the permission
val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
val uri = Uri.fromParts("package", packageName, null)
intent.setData(uri)
startActivity(intent)
}
try {
startActivityForResult(intent, REQUEST_CODE_STORAGE)
} catch (e: Exception) {
Log.e("MainActivity", "Error opening settings: ${e.message}")
Toast.makeText(this, "Unable to open settings for permission. Please enable it manually in settings.", Toast.LENGTH_LONG).show()
}
}
}
// Check if the app has the required storage permissions for Android below API 30
private fun hasStoragePermissions(): Boolean {
return storagePermissions.all {
ContextCompat.checkSelfPermission(this, it) == PackageManager.PERMISSION_GRANTED
}
}
// Handle the results from the permission request
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUEST_CODE_STORAGE) {
if (grantResults.all { it == PackageManager.PERMISSION_GRANTED }) {
readDataFromCSV() // Permission granted, proceed to save data
} else {
Toast.makeText(this, "Permission denied! Cannot save data.", Toast.LENGTH_SHORT).show()
}
}
}
it's working for me
Upvotes: 0
Reputation: 541
Android 10 doesn't require "android.permission.MANAGE_EXTERNAL_STORAGE", android:requestLegacyExternalStorage="true"
under application tag in manifest will work.
For android 11, try this
if (Environment.isExternalStorageManager()) {
//todo when permission is granted
} else {
//request for the permission
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
Upvotes: 44