Skip to main content

Virtual files

Virtual files are an Android-only concept. You have almost surely encountered them in your Google Drive - all the Google Docs, Sheets, Presentations and etc. are virtual files and cannot normally be selected.

note

Picking virtual files is supported on Android 7.0 and above.

Pass allowVirtualFiles: true to the pick function to allow picking virtual files in import mode.

When a virtual file is picked, the isVirtual field is true, and the convertibleToMimeTypes field contains an array of VirtualFileMeta.

This array describes what kind(s) of regular file the virtual file can be exported into - for example, Google docs files can be exported as application/pdf and so the array will be [{ mimeType: 'application/pdf', extension: 'pdf' }].

Obtaining a regular file from a virtual file

If you want to export a virtual file into a local one, use the keepLocalCopy function and

  1. double-check that the fileName parameter includes the extension.
  2. pass a mimeType value to the convertVirtualFileToType parameter.
Picking a virtual file and exporting it to a local one
<Button
title="import virtual file (such as a document from GDrive)"
onPress={async () => {
const [file] = await pick({
allowVirtualFiles: true,
})
const { name, uri: pickedUri, convertibleToMimeTypes } = file

const virtualFileMeta = convertibleToMimeTypes && convertibleToMimeTypes[0]
invariant(name && virtualFileMeta, 'name and virtualFileMeta is required')
const [copyResult] = await keepLocalCopy({
files: [
{
uri: pickedUri,
fileName: `${name}.${virtualFileMeta.extension ?? ''}`,
convertVirtualFileToType: virtualFileMeta.mimeType,
},
],
destination: 'cachesDirectory',
})
if (copyResult.status === 'success') {
const localCopy = copyResult.localUri
// do something with the local copy
}
}}
/>

For viewing or editing of virtual files you'll need to rely on the app that provided the virtual file (for example, Google Docs app for Google Docs files). The Document Viewer module can help you with that.

Learn more about virtual files in this video.