birdofpreyru / react-native-fs

File system access for React Native
https://dr.pogodin.studio/docs/react-native-file-system
Other
160 stars 12 forks source link

RN 0.74.1 - Android 33 - readDir does not seem list the files in RNFS.ExternalStorageDirectoryPath + "/Documents" in an emulator #43

Closed akkumar closed 4 months ago

akkumar commented 4 months ago

Working on

RN 0.74.1

Emulator: - Pixel 8 API 33

Version: Android 33

In Android Studio, use "View" -> Tool Windows -> Device Explorer.

  1. Uploaded couple of files to "/storage/emulated/0/Documents" ( .docx files ) [ sample.docx and sample1.docx ]
  2. Created a new directory as well in that directory ( again using "Device Explorer" )

My AndroidManifest.xml reads as below -

<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" /> 
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" /> 
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" /> 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"  android:maxSdkVersion="29"  />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32"  />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage" />

Updated:

Also have added -

<application ... android:requestLegacyExternalStorage="true" ..> ....

Problem:

In my app, using the RNFS api -

RNFS.readdir(RNFS.ExternalStorageDirectoryPath + "/Documents")

In the list of entries returned by the API - it returns the newly created directory - but not the 2 files.

It gets even more weirder.

If I do a

RNFS.exists(RNFS.ExternalStorageDirectoryPath + "/Documents/sample.docx")

it certainly returns 'true' as it rightfully should.

If that is the case - why would it not return that file in RNFS.readdir(RNFS.ExternalStorageDirectoryPath + "/Documents") api call ?

Weird behavior.

What could I be doing wrong here ?

ps: Yes - trying to create a file manager here for a use case.

akkumar commented 4 months ago

In case it helps further - development on Ubuntu

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04 LTS
Release:    24.04
Codename:   noble
birdofpreyru commented 4 months ago

My guess is your created files have permissions / ownership making them invisible for your app. I don't think there is a bug in the library, as the readdir() implementation for Android is quite straightforward: https://github.com/birdofpreyru/react-native-fs/blob/70c3e0bafddbe98878c7e7dddf1f2acc8b09c64a/android/src/main/java/com/drpogodin/reactnativefs/ReactNativeFsModule.kt#L513-L536, if it does not include some files, it means file.listFiles() at the line 519 doesn't include them into results.

birdofpreyru commented 4 months ago

Also, have you tried to google your problem before asking here? I see there is a bunch of similar questions in the first search results:

And, perhaps, the only way to access files on external storage in Android is to use the system-provided file-picker, i.e. see pickFile() method.