First: there is no way Google will approve this permission for apps which aren’t
- File managers
- Backup and restore apps
- Anti-virus apps
- Document management apps
- On-device file search
- Disk and file encryption
- Device-to-device data migration
based on their Google Play notice
So we are left with SAF, MediaStore… (very limited stuff)
I have an app which can record videos with loop recording option to automatically delete older videos when storage is almost full or when the max space for videos (set by users) is reached.
To record videos with MediaRecorder
I can use SAF or MediaStore, so it’s not a problem, no need in android.permission.MANAGE_EXTERNAL_STORAGE
(Manage all files on a storage device) permission.
So this app which records videos is not suitable for MANAGE_EXTERNAL_STORAGE
permission based on Google policy.
But for loop recording feature I need to know free and total space of the storage, and the directory of videos selected by users (for the max space option).
Luckily I can still use StorageManager to get info about the storages of a device and it seems it doesn’t need any permissions!
Dialogue:
-
“Really? Is StorageManager that great and doesn’t need any permission to get info about space?”
-
“Yes, but only if it’s built-in internal storage or microSD card!!! Though with microSD cards there is no guarantee in some cases”
-
“What about a USB flash drive? I know that Android supports them from the box to read/write files”
-
“HAHA! YOU NEED
MANAGE_EXTERNAL_STORAGE
permission in this case, suffer!”
So my users asks about the possibility to record videos to a USB flash drive, for example they may use it with their car radio (head unit) Android devices or even just Android smartphones.
So I have connected a USB flash drive to my Samsung phone using USB OTG:
And I tried to get info about storages using StorageManager:
fun getStorageInfo(context: Context): List<String> {
val storageManager =
context.getSystemService(Context.STORAGE_SERVICE) as StorageManager
val storages = storageManager.storageVolumes
.mapIndexedNotNull { index, volume ->
buildString {
val file = getVolumeFileRoot(volume)
appendLine("#${index + 1} Storage")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
appendLine("mediaStoreVolumeName: ${volume.mediaStoreVolumeName}")
}
appendLine("uuid: ${volume.uuid}")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
appendLine("storageUuid: ${volume.storageUuid}")
}
appendLine("File.path: $file")
appendLine("File.isDirectory: ${file?.isDirectory}")
appendLine("desc: ${volume.getDescription(context)}")
appendLine("state: ${volume.state}")
appendLine("isRemovable: ${volume.isRemovable}")
appendLine("isEmulated: ${volume.isEmulated}")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
appendLine("isPrimary: ${volume.isPrimary}")
appendLine("allocatedBytes: ${
volume.storageUuid?.let {
storageManager.getAllocatableBytes(it)
}
}")
}
file?.let {
appendLine("File.canRead: ${it.canRead()}")
appendLine("File.canWrite: ${it.canWrite()}")
appendLine("File.freeSpace: ${it.freeSpace}")
appendLine("File.totalSpace: ${it.totalSpace}")
appendLine("File.usableSpace: ${it.usableSpace}")
}
}
}
return storages
}
As you can see I can get info about free/total space only for internal storage but not for a USB flash drive!
So I tried to add <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
to the manifest and grant the permission with:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
&& !Environment.isExternalStorageManager()
) {
startActivity(Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION))
}
}
(which can’t be used for Google Play – won’t be approved by Evil Google)
Started the app and granted the all files permission and now I can access free/total space info of my USB flash drive.
But only me, with a debug app, and not my users!
But it can be even worse – some users reported that it happens (though rarely) with microSD cards as well on some car radio devices without this permission of course, so the info can’t be retrieved just like with USB flash drives. Only internal storage can be used normally. But also mb there were cases with phones and microSD cards as well.
Yeah, sorry, I know that I have 10k reputation on SO already, but come on, enough is enough, some things in Google Android are a joke, let’s admit it, it’s a total disaster.