Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

How to get the absolute path of a file in flutter

I am working on a Flutter project to syntehsise an string to an audio file. For this reason, I have added flutter_tts as a dependency and implemented the following method with different approaches in order to check the existence of the generated file:

/// Synthesises the current audio cue into an audio file
  static Future<void> synthesiseStringToAudioFile() async {
    Future<String> finalPath;
    Future<File> finalFile;
    Uri uriToFile;
    String absolutePath;
    bool existsPath;
    bool existsManually;
    bool exists3;
    await flutterTts
        .synthesizeToFile("This is my first audio synthesizer in Flutter",
            audioFileName)
        .then((value) => {
              // File has been successfully created
              if (value == 1)
                {
                  // Gets the path to the generated audio file
                  finalPath =  pathToFile,
                  finalPath.then((path) async => { 
                        print('AFile :Path to audio file: $path'),
                        // Check if exists
                        existsPath = FileSystemEntity.typeSync(path) != FileSystemEntityType.notFound,
                        print("AFile : Exists? $existsPath"),
                        existsManually = await File('/storage/emulated/0/Android/data/mypath/files/temp_audio_cue.wav').exists(), // Requieres async function
                        print("AFile : Exists2? $existsManually"), // RETURNS TRUE
                        exists3 = await File(path).exists(),
                        print("AFile : Exists3? $exists3")

                  }),

                  // Gets the generated file
                  finalFile = localFile,
                  finalFile.then((file) => {
                    // Absolute path
                    absolutePath = file.absolute.path,
                    print('AFile : AbsolutePath: $absolutePath'),
                    // Check the URI
                    uriToFile = file.uri,
                    print('AFile : URI to audio file: $uriToFile'),
                  }),
                  

                  
                }
              else
                {print('There was an error during the synthezisation')}
            });
  }

  static void setAudioFileName() {
    audioFileName = Platform.isAndroid ? "temp_audio_cue.wav" : "temp_audio_cue.caf";
  }

  /// Gets the path to the file to be accessed
  static Future<String> get pathToFile async {
    final path = await localPath;
    return '$path/$audioFileName';
  }

  /// Gets the path to the local directory
  static Future<String> get localPath async {
    final dir = await getApplicationDocumentsDirectory();
    return dir.path;
  }

Once the synthesisation is completed, flutterTts.synthesizeToFile() logs in console the following message:

D/TTS (10335): Successfully created file : /storage/emulated/0/Android/data/mypath/files/temp_audio_cue.wav

so if I check the existence of the file manually (as I do with existManually) will get a true value, but I am not able to do it trying to get dynamically the path as in the other examples I am trying but the ones I am getting are:

/data/user/0/mypath/app_flutter/temp_audio_cue.wav

so it is missing the beginning

/storage/emulated/0/Android/

I was wondering what is the correct way to get the path to the file (missing)?

question from:https://stackoverflow.com/questions/65901746/how-to-get-the-absolute-path-of-a-file-in-flutter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to get this path : /storage/emulated/0

Use path_provider_ex package, which provides root and app files directory for both "external storage" (internal flash) and SD card (if present), as well as available space for each storage.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...