file picker
a package that allows you to use a native file explorer to pick single or multiple absolute file paths, with extensions filtering support.
file picker
installation
first, add file_picker as a dependency in your pubspec.yaml file.
file_picker: ^1.3.7
android
add
<uses-permission android:name="android.permission.read_external_storage"/>
before <application>
to your app’s androidmanifest.xml
file. this is required to access files from external storage.
ios
based on the location of the files that you are willing to pick paths, you may need to add some keys to your ios app’s info.plist file, located in <project root>/ios/runner/info.plist
:
-
uibackgroundmodes with the fetch and remote-notifications keys – required if you’ll be using the
filetype.any
orfiletype.custom
. describe why your app needs to access background taks, such downloading files (from cloud services). this is called required background modes, with the keys app download content from network and app downloads content in response to push notifications respectively in the visual editor (since both methods aren’t actually overriden, not adding this property/keys may only display a warning, but shouldn’t prevent its correct usage).<key>uibackgroundmodes</key> <array> <string>fetch</string> <string>remote-notification</string> </array>
-
nsapplemusicusagedescription – required if you’ll be using the
filetype.audio
. describe why your app needs permission to access music library. this is called privacy – media library usage description in the visual editor.<key>nsapplemusicusagedescription</key> <string>explain why your app uses music</string>
-
nsphotolibraryusagedescription – required if you’ll be using the
filetype.image
orfiletype.video
. describe why your app needs permission for the photo library. this is called privacy – photo library usage description in the visual editor.<key>nsphotolibraryusagedescription</key> <string>explain why your app uses photo library</string>
note: any ios version below 11.0, will require an apple developer program account to enable cloudkit and make it possible to use the document picker (which happens when you select filetype.all
, filetype.custom
or any other option with getmultifilepath()
). you can read more about it here.
usage
there are only two methods that should be used with this package:
filepicker.getfilepath()
will let you pick a single file. this receives two optional parameters: the filetype
for specifying the type of the picker and a fileextension
parameter to filter selectable files. the available filters are:
filetype.any
– will let you pick all available files.filetype.custom
– will let you pick a single path for the extension matching thefileextension
provided.filetype.image
– will let you pick a single image file. opens gallery on ios.filetype.video
– will let you pick a single video file. opens gallery on ios.filetype.audio
– will let you pick a single audio file. opens music on ios. note that drm protected files won’t provide a path,null
will be returned instead.
filepicker.getmultifilepath()
will let you select multiple files and retrieve its path at once. optionally you can provide a fileextension
parameter to filter the allowed selectable files.
will return a map<string,string>
with the files name (key
) and corresponding path (value
) of all selected files.
picking multiple paths from ios gallery (image and video) aren’t currently supported.
usages
so, a few example usages can be as follow:
// single file path
string filepath;
filepath = await filepicker.getfilepath(type: filetype.any); // will let you pick one file path, from all extensions
filepath = await filepicker.getfilepath(type: filetype.custom, fileextension: 'svg'); // will filter and only let you pick files with svg extension
// pick a single file directly
file file = await filepicker.getfile(type: filetype.any); // will return a file object directly from the selected file
// multi file path
map<string,string> filespaths;
filepaths = await filepicker.getmultifilepath(); // will let you pick multiple files of any format at once
filepaths = await filepicker.getmultifilepath(fileextension: 'pdf'); // will let you pick multiple pdf files at once
filepaths = await filepicker.getmultifilepath(type: filetype.image); // will let you pick multiple image files at once
list<string> allnames = filepaths.keys; // list of all file names
list<string> allpaths = filepaths.values; // list of all paths
string somefilepath = filepaths['filename']; // access a file path directly by its name (matching a key)
a few side notes
- using
getmultifilepath()
on ios will always use the document picker (aka files app). this means that multi picks are not currently supported for photo library images/videos or music library files. - when using
filetype.custom
, unsupported extensions will throw amissingpluginexception
that is handled by the plugin. - on android, when available, you should avoid using third-party file explorers as those may prevent file extension filtering (behaving as
filetype.any
). in this scenario, you will need to validate it on return.
currently supported features
- [x] load paths from cloud files (gdrive, dropbox, icloud)
- [x] load path from a custom format by providing a file extension (pdf, svg, zip, etc.)
- [x] load path from multiple files optionally, supplying a file extension
- [x] load path from gallery
- [x] load path from audio
- [x] load path from video
- [x] load path from any
- [x] create a
file
object from any selected file
if you have any feature that you want to see in this package, please add it here. ��
demo app
example
see example app.
Comments are closed.