Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RNFS.stat doesn't work on directories on Android #540

Closed
searene opened this issue Aug 16, 2018 · 15 comments
Closed

RNFS.stat doesn't work on directories on Android #540

searene opened this issue Aug 16, 2018 · 15 comments

Comments

@searene
Copy link

searene commented Aug 16, 2018

I created a normal file and a directory in RNFS.ExternalStorageDirectoryPath on Android, and applied stat on them respectively.

const statOfFile = await RNFS.stat(RNFS.ExternalStorageDirectoryPath + "/ThisIsAFile");
const statOfDirectory = await RNFS.stat(RNFS.ExternalStorageDirectoryPath + "/ThisIsADirectory");

The first line worked, but the second line threw an exception:

08-16 09:05:28.747  8254  8286 W ReactNativeJS: Error: EISDIR: illegal operation on a directory, read '/storage/emulated/0/ThisIsADirectory'

It seemed to me that I couldn't use stat on directories on Android.

Version:

  • Android: using the android emulator shipped with Android Studio, version: 8.1
  • react-native: 0.55.4
  • react-native-fs: 2.11.15
@tomwir
Copy link

tomwir commented Aug 18, 2018

exacly same problem, and also moveFile got same error

@hejile
Copy link

hejile commented Aug 31, 2018

A sample stacktrace from logcat:

W/System.err( 2875): com.rnfs.IORejectionException: EISDIR: illegal operation on a directory, read '/data/data/com.teach_elders_read/files/tasks'
W/System.err( 2875): at com.rnfs.RNFSManager.getFileUri(RNFSManager.java:75)
W/System.err( 2875): at com.rnfs.RNFSManager.getOriginalFilepath(RNFSManager.java:83)
W/System.err( 2875): at com.rnfs.RNFSManager.stat(RNFSManager.java:535)

@ghost
Copy link

ghost commented Oct 8, 2018

+1

@rp01
Copy link

rp01 commented Oct 24, 2018

Guys, any work around I am not able to fix this issue?

@Weedoze
Copy link

Weedoze commented Oct 24, 2018

I had the same problem with the RFNS.stat method but I found a workaround

Following the RNFS.readDir documentation it will return you an object

type ReadDirItem = {
  ctime: date;     // The creation date of the file (iOS only)
  mtime: date;     // The last modified date of the file
  name: string;     // The name of the item
  path: string;     // The absolute path to the item
  size: string;     // Size in bytes
  isFile: () => boolean;        // Is the file just a file?
  isDirectory: () => boolean;   // Is the file a directory?
};

Thus you can use this method to find files and directories in a folder. You can check for the name of the file or directory to find a specific one

RNFS.readDir(RNFS.ExternalStorageDirectoryPath)
.then(results => {
        const files = results.filter(result => result.isFile());
        const folders = results.filter(result => result.isFolder());
	const myFile = files.find(file => file.name === "ThisIsAFile");
        const myFolder = folders.find(folder => folder.name === "ThisIsAFolder");
})
.catch(err => {
	console.log(err);
});

@rp01
Copy link

rp01 commented Oct 24, 2018

I have tried in following way

RNFS.exists(`${RNFS.CachesDirectoryPath}/TEST`).then((isExist)=>{
            if(isExist){
              RNFS.moveFile("/storage/emulated/0/Pictures/IMG_20181024_181202.jpg",`${RNFS.CachesDirectoryPath}/TEST`).then(console.log).catch(console.error)
            }else{
              RNFS.mkdir(`${RNFS.CachesDirectoryPath}/TEST`,).then((data)=>{
                RNFS.moveFile("/storage/emulated/0/Pictures/IMG_20181024_181202.jpg",`${RNFS.CachesDirectoryPath}/TEST`).then(console.log).catch(console.error)
              })
            }
          })

getting the same error
EISDIR: illegal operation on a directory, read '/storage/emulated/0/Pictures/'

@hejile
Copy link

hejile commented Oct 26, 2018

Guys, any work around I am not able to fix this issue?

you can try my fork https://github.com/hejile/react-native-fs , which fixes 3 bugs. BTW, this project is really buggy.

@itinance
Copy link
Owner

@hejile a PR with fixes is always appreciated :)

@yanivtabibi
Copy link

@hejile Please review my PR for this fix
Thanks

@thisisirfan
Copy link

I have tried in following way

RNFS.exists(`${RNFS.CachesDirectoryPath}/TEST`).then((isExist)=>{
            if(isExist){
              RNFS.moveFile("/storage/emulated/0/Pictures/IMG_20181024_181202.jpg",`${RNFS.CachesDirectoryPath}/TEST`).then(console.log).catch(console.error)
            }else{
              RNFS.mkdir(`${RNFS.CachesDirectoryPath}/TEST`,).then((data)=>{
                RNFS.moveFile("/storage/emulated/0/Pictures/IMG_20181024_181202.jpg",`${RNFS.CachesDirectoryPath}/TEST`).then(console.log).catch(console.error)
              })
            }
          })

getting the same error
EISDIR: illegal operation on a directory, read '/storage/emulated/0/Pictures/'

Hi, Did you found any solution for this error ?

@ghost
Copy link

ghost commented Oct 25, 2019

I have tried in following way

RNFS.exists(`${RNFS.CachesDirectoryPath}/TEST`).then((isExist)=>{
            if(isExist){
              RNFS.moveFile("/storage/emulated/0/Pictures/IMG_20181024_181202.jpg",`${RNFS.CachesDirectoryPath}/TEST`).then(console.log).catch(console.error)
            }else{
              RNFS.mkdir(`${RNFS.CachesDirectoryPath}/TEST`,).then((data)=>{
                RNFS.moveFile("/storage/emulated/0/Pictures/IMG_20181024_181202.jpg",`${RNFS.CachesDirectoryPath}/TEST`).then(console.log).catch(console.error)
              })
            }
          })

getting the same error
EISDIR: illegal operation on a directory, read '/storage/emulated/0/Pictures/'

Hi, Did you found any solution for this error ?

It seems you have no rights to read the directory "/storage/emulated/0/Pictures/" or it does not exists I think.

@thisisirfan
Copy link

thisisirfan commented Oct 25, 2019

@NeliHarbuzava But i have already granted user for External Read And Write Permission and i'm able to get all files inside these directories but not able to copy them

@thisisirfan
Copy link

thisisirfan commented Oct 25, 2019

I have tried in following way

RNFS.exists(`${RNFS.CachesDirectoryPath}/TEST`).then((isExist)=>{
            if(isExist){
              RNFS.moveFile("/storage/emulated/0/Pictures/IMG_20181024_181202.jpg",`${RNFS.CachesDirectoryPath}/TEST`).then(console.log).catch(console.error)
            }else{
              RNFS.mkdir(`${RNFS.CachesDirectoryPath}/TEST`,).then((data)=>{
                RNFS.moveFile("/storage/emulated/0/Pictures/IMG_20181024_181202.jpg",`${RNFS.CachesDirectoryPath}/TEST`).then(console.log).catch(console.error)
              })
            }
          })

getting the same error
EISDIR: illegal operation on a directory, read '/storage/emulated/0/Pictures/'

Hi, Did you found any solution for this error ?

It seems you have no rights to read the directory "/storage/emulated/0/Pictures/" or it does not exists I think.

BTW Am i request correct permission ?
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE & PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE

@thisisirfan
Copy link

I have tried in following way

RNFS.exists(`${RNFS.CachesDirectoryPath}/TEST`).then((isExist)=>{
            if(isExist){
              RNFS.moveFile("/storage/emulated/0/Pictures/IMG_20181024_181202.jpg",`${RNFS.CachesDirectoryPath}/TEST`).then(console.log).catch(console.error)
            }else{
              RNFS.mkdir(`${RNFS.CachesDirectoryPath}/TEST`,).then((data)=>{
                RNFS.moveFile("/storage/emulated/0/Pictures/IMG_20181024_181202.jpg",`${RNFS.CachesDirectoryPath}/TEST`).then(console.log).catch(console.error)
              })
            }
          })

getting the same error
EISDIR: illegal operation on a directory, read '/storage/emulated/0/Pictures/'

Hi, Did you found any solution for this error ?

It seems you have no rights to read the directory "/storage/emulated/0/Pictures/" or it does not exists I think.

BTW Am i request correct permission ?
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE & PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE

@NeliHarbuzava I managed to fixed it, it was silly mistake. Thanks or help though
https://github.com/itinance/react-native-fs/issues/792

@stepupithubapp
Copy link

stepupithubapp commented Nov 16, 2019

I have tried in following way

RNFS.exists(`${RNFS.CachesDirectoryPath}/TEST`).then((isExist)=>{
            if(isExist){
              RNFS.moveFile("/storage/emulated/0/Pictures/IMG_20181024_181202.jpg",`${RNFS.CachesDirectoryPath}/TEST`).then(console.log).catch(console.error)
            }else{
              RNFS.mkdir(`${RNFS.CachesDirectoryPath}/TEST`,).then((data)=>{
                RNFS.moveFile("/storage/emulated/0/Pictures/IMG_20181024_181202.jpg",`${RNFS.CachesDirectoryPath}/TEST`).then(console.log).catch(console.error)
              })
            }
          })

getting the same error
EISDIR: illegal operation on a directory, read '/storage/emulated/0/Pictures/'

Hi, Did you found any solution for this error ?

It seems you have no rights to read the directory "/storage/emulated/0/Pictures/" or it does not exists I think.

BTW Am i request correct permission ?
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE & PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE

@NeliHarbuzava I managed to fixed it, it was silly mistake. Thanks or help though
https://github.com/itinance/react-native-fs/issues/792
hey ??? how to copy file i'm etting error of EISDIR: illegal operation on a directory, read '/storage/emulated/0/Pictures'

@thisisirfan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants