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

Android: add pickDirectory #414

Merged
merged 17 commits into from
Jun 15, 2021

Conversation

roman-r-m
Copy link
Contributor

@roman-r-m roman-r-m commented May 22, 2021

Resolves #302

Adds a method to show system directory picker on Android. Returns the URI of the directory and if possible resolves the URI to the actual path.

  • I have tested this on a device and a simulator
  • I added the documentation in README.md
  • I updated the typed files (TS and Flow)

@roman-r-m
Copy link
Contributor Author

Tested on a device but it does not have a slot for SD card, so that part I have only tested in simulator

@roman-r-m roman-r-m marked this pull request as ready for review May 22, 2021 12:41
@roman-r-m
Copy link
Contributor Author

Ping?

@vonovak
Copy link
Collaborator

vonovak commented Jun 9, 2021

hi, I am sorry, I'll test this out this week! :)

index.js Show resolved Hide resolved
index.d.ts Outdated Show resolved Hide resolved
// internal: content://com.android.externalstorage.documents/tree/primary%3Atest
// sd card: content://com.android.externalstorage.documents/tree/1DEA-0313%3Atest
List<String> pathSegments = uri.getPathSegments();
if (pathSegments.get(0).equalsIgnoreCase("tree")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you write this code by yourself, or is it copied from somewhere (where?)

I'm worried this can vary a lot among android versions and I'm not sure this would work reliably... 🤔

Copy link
Contributor Author

@roman-r-m roman-r-m Jun 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote it myself. I could only test it with emulator and my phone (Honor 9) which does not have external sd card.

Copy link
Collaborator

@vonovak vonovak Jun 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello and thanks for the reply :) I think I need more context to understand.

What is the reason for returning the path from this call? Why not return only the uri?

If the reason is that you want to write a file to that path later on, then I believe the right approach to do this is outlined here https://stackoverflow.com/a/61120265/2070942 and should not be handled in this library.

The approach taken here seems like a heuristic that works for some cases, but may not work in others so I'm hesitant to merging this. We should be using android-system-provided tools to work with uris.

If the only thing returned from the call were the uri, then I'd be happy with this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for returning the path from this call? Why not return only the uri?

This is so that you can then use react-native-fs to access the directory. It may stop working in Android 12 but I suppose it's better than nothing.

If the reason is that you want to write a file to that path later on, then I believe the right approach to do this is outlined here https://stackoverflow.com/a/61120265/2070942 and should not be handled in this library.

Sure, but then you won't be using react-native any more and would be just writing a native app.

If the only thing returned from the call were the uri, then I'd be happy with this.

As far as I'm aware you can't do anything with that URI from RN, without going native.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach taken here seems like a heuristic that works for some cases, but may not work in others so I'm hesitant to merging this. We should be using android-system-provided tools to work with uris.

Ok, fair enough.

index.d.ts Outdated Show resolved Hide resolved
@roman-r-m roman-r-m closed this Jun 14, 2021
@roman-r-m roman-r-m deleted the android-pick-dir branch June 14, 2021 08:41
@vonovak
Copy link
Collaborator

vonovak commented Jun 14, 2021

Hello @roman-r-m, sorry if I made it sound like I am against merging this PR - I am not. The only thing I do not fancy is the part about coverting the uri to path.

I do believe it makes sense to choose a folder using this lib and return its uri. Whatever happens with the uri next is up to the library user.

I do not believe we should do anything more - eg. If you want to write a file to the uri using some library (eg. React Native fs) then that library should handle the job of converting that uri to a path eventually.

Hope this makes sense, thanks!

@roman-r-m
Copy link
Contributor Author

Ok, thanks. I'll resubmit without the path.

@roman-r-m roman-r-m reopened this Jun 15, 2021
index.d.ts Outdated Show resolved Hide resolved
@vonovak vonovak merged commit 4173790 into react-native-documents:master Jun 15, 2021
@vonovak
Copy link
Collaborator

vonovak commented Jun 15, 2021

thank you for the PR! :)

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

Successfully merging this pull request may close these issues.

FR: Select Folder Location
2 participants