Skip to content Skip to sidebar Skip to footer

Android Api Level 23, How To Read Files On Emulator?

I am trying to list all the files in Downloads folder of an emulator which has an SDCard. Now when I do this File file = Environment.getExternalStorageDirectory(); the value of

Solution 1:

The File.listFiles() method

Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

Most likely file denotes a directory. Maybe it just doesn't exist yet or a permission is denied, causing an I/O error.

18E2-1B1D is just an identifier for the SD card, so you shouldn't have to worry about this. In the end emulator and devices should work the same if you're only using high level APIs.


But in Android api level 23 the permission model changed. Now you need to request permissions at runtime. The call

checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)

should return PackageManager.PERMISSION_GRANTED. If not, you'll need to call

requestPermissions(newString[] {Manifest.permission.READ_EXTERNAL_STORAGE}, id);

and handle the result asynchronously in onRequestPermissionsResult (int, String[], int[]).

The permission still needs to be defined in the manifest. If you want to use the WRITE_EXTERNAL_STORAGE instead, just adapt it in the code as well.

Post a Comment for "Android Api Level 23, How To Read Files On Emulator?"