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

SAF Support #926

Open
crutchcorn opened this issue Sep 9, 2020 · 5 comments
Open

SAF Support #926

crutchcorn opened this issue Sep 9, 2020 · 5 comments

Comments

@crutchcorn
Copy link

crutchcorn commented Sep 9, 2020

Does the project have any ability to support SAF read/writes? I've tried using a SAF Directory Picker I wrote:

https://github.com/oceanbit-dev/react-native-directory-picker/blob/master/android/src/main/java/com/rndirectorypicker/DirectoryPickerModule.java

But whenever utilizing the writeFile or readFile, I seem to always get errors since the paths cannot be converted from SAF to /typical/paths/like/this. SAF is required as-of Android R, and once React Native apps hit API 30 support, apps that aren't using SAF will likely break (such as mine).

Here's the relevant code to outline what I'm doing:

  const [safPath, setSafPath] = React.useState('');

  const pickSAFPath = () => {
    NativeModules.DirectoryPickerSAFModule.selectDirectory((path) =>
      setSafPath(path),
    );
  };

  const writeToFileSAF = () => {
    RNFS.writeFile(`${safPath}/test.txt`, 'Lorem ipsum dolor sit amet', 'utf8')
      .then(() => {
        console.log('FILE WRITTEN!');
        showAlert();
      })
      .catch((err) => console.error(err));
  };

Then, when I run writeToFileSAF, I get an error similar to the following:

[Error: ENOENT: Failed to open for writing: java.io.FileNotFoundException: open failed: EISDIR (Is a directory), open 'content://com.android.externalstorage.documents/tree/primary%3ADocuments/document/primary%3ADocuments/test.txt']

image

Are there known bugs regarding SAF? What's the future of this package look like?

@crutchcorn
Copy link
Author

I have also confirmed that even without using a directory picker, the following paths do not work after targeting past API 29:

  • ExternalDirectoryPath
  • ExternalStorageDirectoryPath

When I try to run either of these interactions with the following code:

  const path = RNFS.ExternalStorageDirectoryPath  + '/images/test.txt';

  const writeToFile = () => {
    RNFS.mkdir(RNFS.ExternalStorageDirectoryPath  + '/images')
      .then(() => RNFS.writeFile(path, 'Lorem ipsum dolor sit amet', 'utf8'))
      .then((success) => {
        console.log('FILE WRITTEN!');
      })
      .catch((err) => {
        console.error(err.message);
      });
  };

Which worked on the same emulated Android R device when built using API 28

I get the following error:

[Wed Sep 09 2020 20:04:40.864]  ERROR    Directory could not be created

image

However, interestingly, the following DOES work:

  • DownloadDirectoryPath

@tolgaerdonmez
Copy link

@crutchcorn So does it only work on, DownloadDirectoryPath, I mean can just use DownloadDirectoryPath to write and read files ?

@crutchcorn
Copy link
Author

crutchcorn commented May 3, 2021

@tolgaerdonmez yes, that's correct. You can create sub-folders that way. Honestly, SAF is a pretty big can of worms that requires a dedicated API. I might start a separate package just for SAF support

@gakshat14
Copy link

@crutchcorn so were you able to read files from the download directory? Because as far as I know we can't read the files from download directory.

@crutchcorn
Copy link
Author

It seems to work for me. This is the code I'm using to do the write:

https://github.com/oceanbit/GitShark/blob/upgrade-deps/src/components/error-prompt/error-prompt-common.tsx#L55

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

3 participants