Document picker
DocumentPicker opens the system's file picker, bound to expo-document-picker: the Files app's
browser on iOS and the storage access framework on Android.
import { DocumentPicker } from '@ng-native/expo';
inject(DocumentPicker)Platforms. Available on macOS, Linux, iPadOS, iPhone and Android. Absent on Web host, where available() is false.
The options are the module's own, passed through unchanged. A canceled picker answers with no files rather than a result to unwrap, and the system picker needs no permission, so none is asked for.
Install
npx expo install expo-document-pickerimport { DocumentPicker } from '@ng-native/expo/document-picker';The smallest useful example
import { Component, inject, signal } from '@angular/core';
import { Pressable, Text } from '@ng-native/components';
import { DocumentPicker } from '@ng-native/expo/document-picker';
@Component({
selector: 'app-attach',
imports: [Pressable, Text],
template: `
<pressable (press)="attach()"><text>Attach a PDF</text></pressable>
<text>{{ attached() }}</text>
`,
})
export class Attach {
private readonly documents = inject(DocumentPicker);
protected readonly attached = signal('');
protected async attach(): Promise<void> {
const [file] = await this.documents.pick({ type: 'application/pdf' });
if (file) this.attached.set(file.name);
}
}To read what was picked, open its uri with FileSystem:
await this.files.file(file.uri).text(), with files = inject(FileSystem) as a field of the
component, or .bytes() for anything that is not text.
What it does
pick(options?)- opens the picker and resolves to the files chosen, empty if the user canceled. Each is the module's ownDocumentPickerAsset:uri,name,mimeType,sizeandlastModified.type- a MIME type such as'image/*', or a list of them. Everything by default.multiple- allow more than one file.copyToCacheDirectory- on by default: each file is copied into the app's cache, so the file system and other Expo modules can read itsuri. Off leaves the platform's own reference, which only some readers understand.base64- on the web, whether eachuriis the file's contents as base64. On by default.
pickDirectory()- opens the system folder picker and resolves to the folder chosen, so a file can be saved somewhere the user can find it, such as Files on iOS or a Drive folder on Android.nullwhen the user dismisses it, and where the platform has no folder picker; any other failure is rethrown. It usesexpo-file-system's picker, so that package must be installed too. Make a file in the folder withcreateFile(name, mimeType), then write it withFileSystem.write(). On macOS it is the desktop host's open panel, and on Linux the desktop's folder chooser, andcreateFilemakes the file in the folder through the desktop host.save(options)- writes a file where the user chooses and resolves to where it went, ornullif they dismissed the dialog or there is nowhere to save. On macOS it is the save panel, and on Linux the desktop's save dialog; on iOS and Android the user picks a folder and the file is made in it.suggestedName- the name to give the file, with its extension.contents- text, or bytes for anything that is not text. OrfromFile, a file to copy, such as one from the file system.types- the MIME types it may be saved as, the first the default.
const where = await this.documents.save({
suggestedName: 'export.csv',
contents: csv,
types: ['text/csv'],
});On Linux the GTK host's dialogs are the desktop's FileChooser portal, so they look like the
desktop's own (GNOME's or KDE's), placed against the app's front window. Where no portal answers,
they are GTK's own GtkFileDialog. The types a call names become one filter of all of them.
Without the module
On iOS and Android, a missing expo-document-picker - never installed, or installed without the app
being rebuilt since - leaves the service made, so injecting it does not throw. A call, or a read of
a value only the module knows, throws a MissingModuleError. Its message names the module and the
commands that fix it; see
Using a module.
On the web, and in a test that provides no fake, pick() resolves to an empty list and
pickDirectory() and save() to null.
Reference
DocumentPickerservice@ng-native/expoimport { DocumentPicker } from '@ng-native/expo';Methods
pick(options?: DocumentPickerOptions): Promise<readonly PickedDocument[]> Opens the system picker. type is a MIME type or a list of them, multiple allows more than one file, and copyToCacheDirectory (the default) copies each into the app's cache so other Expo modules can read it. Empty if they cancelled.
pickDirectory(): Promise<PickedDirectory | null> Opens the system folder picker and answers the folder the user chose, or null if they dismissed it, and where the platform has no folder picker. Create a file in it with createFile(name, mimeType) , then write it with FileSystem.write() . Any other failure is rethrown. Needs no permission.
save(options: SaveOptions): Promise<string | null> Writes a file where the user chooses, and answers where it went, or null if they dismissed the dialog. contents is text or bytes; fromFile copies another file, such as one in the cache. On macOS it is the save panel; on iOS and Android the user picks a folder and the file is made in it with suggestedName . Null where there is neither.