0

With the introduction of scope storage that works for media files such as image, audio, and video. How to perform permission checking with without specifying file type in a custom folder? Example directory is /storage/emulated/0/custom_folder containing a non media file.

With media files I can do it with these code.

fun hasImageStoragePermission(context: Context): Boolean {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
        ContextCompat.checkSelfPermission(context, Manifest.permission.READ_MEDIA_IMAGES) ==
                PackageManager.PERMISSION_GRANTED
    else
        hasStoragePermission(context)
}

private fun hasStoragePermission(context: Context): Boolean {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        ContextCompat.checkSelfPermission(context, Manifest.permission.READ_MEDIA_IMAGES) ==
                PackageManager.PERMISSION_GRANTED
    } else {
        true // Android 5.1 and below has no runtime permission thus return true
    }
}

But in our case I am trying to read file like this

fun getFileString() : String? {
    val items: String
    try {
        items = File("/storage/emulated/0/custom_folder/file.txt").bufferedReader().use { it.readText() }
    } catch (e: IOException) {
        e.printStackTrace()
        return null
    }
    return items
}
Bitwise DEVS
  • 2,858
  • 4
  • 24
  • 67
  • `Example directory is /storage/emulated/0/custom_folder` That is an impossible example as your app on Android 11+ devices can not create 'custom_folder' in root of external storage. Please elaborate your problem. – blackapps Aug 18 '23 at 05:31
  • `READ_IMAGE_STORAGE` Never heard of that permission. – blackapps Aug 18 '23 at 05:33
  • `in our case I am trying to read file like this` That code tries to read a directory. – blackapps Aug 18 '23 at 05:35
  • You are not telling what goes wrong? What is your problem exactly? – blackapps Aug 18 '23 at 05:35
  • `How to perform permission checking with regular files` Impossible. You can only check if you have a permission. That is independent of actual files. – blackapps Aug 18 '23 at 05:40
  • Dont mess with permissions. Just try to read the file. If you cant you have no permission. And read my first comment. You elaborated nothing. You did not even react on that comment. – blackapps Aug 21 '23 at 09:34
  • @blackapps Hmm, actually it is not impossible in our case since the one that is writing files in root is the hardware provider itself that has modified android os installed on it. The feature is called POS terminal parameters which behaves much like Firebase Remote Config. I don't know but you seems very mad for some reason? Chill – Bitwise DEVS Aug 24 '23 at 02:27

0 Answers0