How to send user out of the application - To a specific directory path

372

I seem to have found a solution for your question with a package called open_file. You can simply point to the path you want to open and it will ask the OS for the app you want to use to handle files or use what is set as your default.

Basic code example:

class OpenDirectoryPath extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: RaisedButton(
        child: Text('Open path'),
        onPressed: () {
          _openPath('/storage/emulated/0/Download/');
        },
      ),
    );
  }

  void _openPath(String path) async {
    OpenFile.open(path);
  }
}

(tested in Android)

Share:
372
Houssem
Author by

Houssem

Houssemeddine is a professional developer who is always trying to learn new things. When he is free, he designs a new feature for DevBrains.

Updated on December 24, 2022

Comments

  • Houssem
    Houssem over 1 year

    I have a specific path where user files are exported under the following path:
    e.g : /storage/emulated/0/Android/data/com.example.app_name/files
    However the problem is not how generate the path but how to do this redirection (open the folder where the file is stored). I've tried to use the plugin url_launcher but apparently it doesn't work.I tried to launch this path as url:
    code example :

    FlatButton(
        onPressed: () async{
            // _localPath returns /storage/emulated/0/Android/data/com.example.app_name/files 
            final path = await _localPath;
            if (await canLaunch(path)) {
                await launch(path);
            } else {
                throw 'Could not launch $path';
              }
            },
        child: Text('Navigate to App storage Folder'),
          ),
    

    The user exported the data as csv file and he want to use that file.
    The expected result : user will be redirected to the path (folder files) and exit the Flutter application. PS : I'm targeting only Android Platform.

    • Sajjad
      Sajjad over 3 years
    • Sajjad
      Sajjad over 3 years
    • Houssem
      Houssem over 3 years
      Those questions about openning files, my question is : How to redirect the user out of the application without openning the file.
    • J. S.
      J. S. over 3 years
      @Houssem Have you been to try my answer?
    • Houssem
      Houssem over 3 years
      @JoãoSoares I'm trying,now still can't access to the android/data/com.example.../files, i'll accept it just let me verify some things so I can also provide useful informations about the problem.For now I think we can't access ../Android/data/.. so as an alternative i'll try to do the work in Download Folder.
    • J. S.
      J. S. over 3 years
      Some directories might be protected. Are you trying to access files inside your app itself? (based on the path /com.example...)
    • Houssem
      Houssem over 3 years
      Yes under my app files.
    • J. S.
      J. S. over 3 years
      If I understood correctly, you are trying to access files that are inside your actual app package? Someone who knows more about Android should correct me, but I believe those are protected and you can't access them.
    • J. S.
      J. S. over 3 years
      @Houssem Does working on the Downloads folder work for you?
    • Houssem
      Houssem over 3 years
      @JoãoSoares I tried it in phone storage under a folder that I created and stored the file there and it works but the file manager doesn't go to the provided path but a default one.Also I stored the file under ..app/files but when i access the file manager doesn't suggest the correct path but a default one too (categories) but idk it depends on the phone model maybe cause the result on emulator and my testing phone not the same.also i want to mention that there is changes in the upcoming android 11 : developer.android.com/about/versions/11/privacy/…
    • J. S.
      J. S. over 3 years
      This issue is different from the original question, which was regarding sending the user from an app to a specific folder. So if the answer solved your issue, please mark it as correct and ask a new question regarding restricted directories in devices.
    • Houssem
      Houssem over 3 years
      The issue still the same now that OpenFile.open(my_path) doesn't open the right path but give a suggestion how to continue and if you continue with File Manager It opens a default path and not the given path.it does not open the given path where the file stored even if the stored file is directly under storage /sdcard/0/MyFolder/.
  • Houssem
    Houssem over 3 years
    I find your answer useful but it's not a definitve answer since The File Manager doesn't open the given path.but a default path that the File Manager will provide.Paths i tried : under files folder of my app and directly under a custom folder in phone storage.
  • J. S.
    J. S. over 3 years
    It seems that I made the mistake of using the default Download directory as the example, and the Android File Manager was already open on that same folder. So it's simply switching the apps. I am starting to really believe this is not possible to do unless the File Manager app accepts a parameters that tells it which path you want to send the user to. And since you can't control what app the user will be utilizing, then you can't do this.