0

I come to you in a time of great need. I am currently learning to use Kotlin for app development and as a "project" per-say, I am working on a simple "File manager". The current problem I am experiencing is that I am unable to read the directories and the files.

  • Using API 26
  • Using Kotlin
  • Using ViewModel
  • The permissions in the AndroidManifest.xml are set
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  • The permission request in runtime is called in MainActivity

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if (checkPermission()) {
            //permission allowed
            val path = Environment.getExternalStorageDirectory().path
            val filesAndFolders: Array<File>? = File(path).listFiles()
            Log.d("FILETAG", path) // /storage/emulated/0
            Log.d("FILETAG", filesAndFolders.toString()) // null
            Log.d("FILETAG", File(path).exists().toString()) // true
            Log.d("FILETAG", File(path).canRead().toString()) // false
        } else {
            //permission not allowed
            requestPermission()
        }

        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                    .replace(R.id.container, MainFragment.newInstance())
                    .commitNow()
        }
    }
    

}

    private fun checkPermission(): Boolean {
        val result = 
            ContextCompat.checkSelfPermission(
                this,
                android.Manifest.permission.READ_EXTERNAL_STORAGE
            )
        return result == PackageManager.PERMISSION_GRANTED
    }

    private fun requestPermission(){
        if(ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)){
            Toast.makeText(this, "Storage permission is required", Toast.LENGTH_SHORT).show()
        } else {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
                111
            )
        }
    }

As commented in the code, the file array is returned as a "null", though the files seem to exist but are unreadable.

Additionally, I have tried executing this code from an inside of the fragment, but with the exact same results, though am required to read the files in a fragment rather than inside the MainActivity (But I first need to get this part of my code working before I move on to the fragments) and list the files in a RecyclerView.

This is my first question on Stackoverflow, if I missed any essential detail, let me know.

Please grant me your infinite knowledge, thank you.

  • What phone do you have? Are you sure you want to print a directory list? Maybe you don't have rights to do that. – CoolMind Oct 21 '22 at 07:05
  • @CoolMind I am testing on a physical device, specifically on the "Xiaomi Mi 9T", I can confirm that the permission is granted in the Settings/Apps/Manage Apps/Permissions/Permissions Manager – Phoenonical Oct 21 '22 at 07:07
  • What if add `"/Pictures"` or `"/Download/"` to `path` as written in https://stackoverflow.com/questions/8646984/how-to-list-files-in-an-android-directory? Do you have Android 8? If >= 10, you should also add ``. – CoolMind Oct 21 '22 at 07:12
  • @CoolMind Adding `` seems to have completely fixed my problem, I can now read the entire directory. Thank you very much! – Phoenonical Oct 21 '22 at 07:26
  • Good! You can read more about this tag. Accessing files has changed in Android 10, so probably there are more tools. – CoolMind Oct 21 '22 at 07:47

0 Answers0