'Got FileNotFoundException (Permission denied) when read files under FilesDir on AVD

As doc said, reading files under /data/user/0/packagename/files/ dosen't require asking Read permission.

I created an emulator(api25) using android studio, uploaded a file named "abc.zip" to the files dir(using Device File Explorer in Android Studio), and then run the following code:

private fun readFromFiles() {
    
    val readBytes = this.filesDir.listFiles()?.filter {
        it.name.equals("abc.zip", true)
    }?.get(0)?.inputStream()?.readBytes()

    Log.d(TAG, "readFromFiles: ${readBytes?.size}")
    
}

I got an error in logcat said:

java.io.FileNotFoundException: /data/user/0/my.package.name/files/abc.zip (Permission denied)

With same code and same file, but on an avd of api29, no error found and I can read the file.

This is weired. So is it a bug of AVD, or did I misunderstand anything?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source