0

just wondering if there is any way to access files on the system. not just the directories provider by path_provider?

seems like this should be part of core flutter, or at least a package. maybe most flutter apps don't require files from elsewhere (or maybe most apps in general, I'm just starting out)

I want to scan the file system for audio files. I know I could do it in android, as I made a little demo app to try some things with native code

thejacer87
  • 13
  • 1
  • 8
  • Have you seen https://stackoverflow.com/questions/49702454/how-do-i-open-a-file-from-disk-in-a-flutter-app ? – Gabriel Devillers Dec 19 '18 at 17:23
  • yes, but it just recommends using path_provider, which isn't enough for me. It mentions the file system in the answer: "You can directly access the file system but due to differences in android and iOS you'll probably end up using the path provider plugin" but doesn't explain it there. perhaps i will see if they can expand a bit. I assume they will say to write custom code for each platform. Which to me, should have been exactly what the path_provider plugin should do. – thejacer87 Dec 19 '18 at 22:59

1 Answers1

1

The issue is that each platform is very much different, both in what you're able to do and how files are arranged.

The path_provider does actually have a way of getting the external files directory: getExternalStorageDirectory. However, that only works on Android as iOS doesn't allow reading files outside the app's sandbox. But once you use that you can use dart:io's File and Directory classes to read the files.

You also need permission on android to be able to read files. Putting this in your manifest should do it, but there's tons of android-specific answers that will tell you more.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Note that on iOS, there are ways to access specific types of files (i.e. pictures and videos), just no normal filesystem access.

Also note that if you've already done in android native code, you can use platform channels to interface between flutter and the native code.

rmtmckenzie
  • 37,718
  • 9
  • 112
  • 99
  • right, im trying to write an app for both platforms, i want to access song files on the phones. whether they are in the sd card, saved in the downloads dir, or where ever iphone would store the songs the users own. the `getExternalStorageDirectory` can't access the "downloads" directory, right? i want to get the paths of audio the user may have downloaded and yes, i saw the platform channels, and realize that i may just have to write a custom plugin to achieve this. was just hoping for an easier solution. probably a good idea for me to make an issue on the path_provider issue queue – thejacer87 Dec 20 '18 at 00:31
  • getExternalStorageDirectory does allow you to get the Downloads directory etc on Android - it actually calls `Environment.getExternalStorageDirectory().getAbsolutePath()`. So the downloads folder would be getExternalStorageDirectory() + /Downloads on android only. For iOS, I don't know if what you're talking about is really possible. Everything is so sandboxed that it's unlikely you'll be able to access anything. However, there is the Apple Music API, which could do some of what you're looking for but you'd have to evaluate that. You'd almost for sure have to write some native code though – rmtmckenzie Dec 20 '18 at 00:42
  • k thanks for the help. basically i just want to replicate somehow the https://github.com/google/ringdroid app for both platforms, with flutter. i guess i will look into the apple music api. i assume as long as i have the path to the song, the bytes can be loaded, then manipulated – thejacer87 Dec 20 '18 at 00:48
  • 1
    I doubt the apple music API will give you access to the binary files - it's more likely you'd be able to tell it to play or stop but not actually get the music data. – rmtmckenzie Dec 20 '18 at 00:50
  • so it does look like it is possible: https://fnd.io/#/us/search?mediaType=ios&term=ringtone i tried a few of the top ones, and they are very similar too github.com/google/ringdroid. the one thing is, while ringdroid just saves the ringtone to your phone, and you can easily, each of these ios apps show you a video, like this: https://www.youtube.com/watch?v=6FsDvf8ldhc showing you how to use it as a ringtone seems stupidly convoluted, but hey, thats apple – thejacer87 Dec 21 '18 at 05:54