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

Not getting the absolute path for any file in storage. #411

Closed
naman-jn opened this issue Sep 26, 2020 · 3 comments
Closed

Not getting the absolute path for any file in storage. #411

naman-jn opened this issue Sep 26, 2020 · 3 comments

Comments

@naman-jn
Copy link

naman-jn commented Sep 26, 2020

@miguelpruivo
I am facing the same issue even in the latest version. For any file from the storage even using default file explorer a temporary file is being created in my app folder and this file is being returned

Thanks for the response. In my case, I am trying to load a file created by a third party game and stored in the phone's Documents folder (on Android). This is the path to the file on my phone:

/storage/emulated/0/Documents/Sports Interactive/Football Manager 2019 Mobile/normal/games/fm_save4.dat

It's not a remote URI, it's on the phone, but file_picker is caching it to:

/data/user/0/com.example.fmm_app/cache/fm_save4.dat

I want to be able to store the direct path, so when the user plays the game and the file changes, they can reload it through my app without needing to go through file_picker again (as it's relatively slower and more clicks etc). The cached version is out of date as soon as they play the game again, so is useless.

If I hard code in the direct path, I can open file, so it seems I have permissions to access it even though it's in a Documents folder belonging to another app. Is there anyway file_picker can provide this path? Is there some sort of bug preventing it? Or more likely, something I'm missing? Thanks!

Originally posted by @Stephen-Cronin in #40 (comment)

@miguelpruivo
Copy link
Owner

miguelpruivo commented Sep 26, 2020

Hi @naman-jn, this have been explained multiple times and unfortunately that’s how Android team want (see #398 answer for example).

Accessing absolute paths from anywhere on the device, being it a SD card or internal storage, may result in undesired effects as sometimes paths just can’t be retrieved at all. Since scoped storage was introduced with Android 10, this gets even worse and you’d not be happy to have your users not having reliable ways to pick the files, this is, some devices could actually pick whereas others don’t.

There are 2 main approaches currently that Android docs suggest:

  1. Pick files for CRUD operations (read, delete, edit) through files URI and use it directly — this is what you actually want but unfortunately isn’t supported by Flutter as it needs an absolute path to open a File descriptor;
  2. Cache the file temporarily for upload or similar, or just copy into your app’s persistent storage so you can later access it — this is what’s being done currently and even though you may have an additional step moving/copying the file after first picking, makes it safer and reliable to access any allowed file on any Android device.

TL;DR: after picking the file the first time, just move it to your app storage scope and use it from there onwards (you can use getApplicationDocumentsDirectory() method from path_provider to get your apps local storage.

Have in mind that currently file_picker is optimised to the point where if you pick twice the same file (with the same name) you’ll be probably picking the previous cached file, as it only will be cached the first time is picked.

However, this issue give an idea of adding another flag property when picking which is making the files persistent or not after picking, which would prevent this additional step for those that actually want it.

@naman-jn
Copy link
Author

Thanks !! Well is there a way where I can get the absolute path as I need to make changes to the real file from my app?
I tried the URI approach but couldn't really achieve it.

@miguelpruivo
Copy link
Owner

@naman-jn you can move the file like I said to your apps directory after picking or, as a last resource, you can use any version of file_picker below 2.0.0, which does exactly what you’re looking for, picking the the real absolute path, but be aware of what I’ve mentioned before.

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

2 participants