Skip to main content

Keeping a local copy of the picked files

keepLocalCopy makes the file available in the app's storage. The behavior is different on iOS and Android:

This method opens an InputStream pointing to the picked content:// uri (from both Open and Import modes) and stores its bytes into a file - i.e. it can be used to "convert" a content:// Uri into a local file. This file's location is determined by the destination parameter.

It also "converts" virtual files (such as Google Docs or sheets) into local files.


note

For each call of keepLocalCopy, a new unique directory is created in the app's storage, and the files are placed into it.

This way, the files are isolated and subsequent calls to keepLocalCopy with the same file names do not overwrite the previous files.

When writing to the filesystem, path traversal vulnerability is prevented. Writing files outside the intended destination will error.

Example: keeping a local copy of the picked file
import { pick, keepLocalCopy } from '@react-native-documents/picker'
return (
<Button
title="single file import, and ensure it is available in the local storage"
onPress={async () => {
try {
const [{ name, uri }] = await pick()

const [copyResult] = await keepLocalCopy({
files: [
{
uri,
fileName: name ?? 'fallback-name',
},
],
destination: 'documentDirectory',
})
if (copyResult.status === 'success') {
// do something
}
} catch (err) {
// see error handling
}
}}
/>
)