目录

Saving User Files

When a user needs to download a file from the Internet or save a file to another directory, use FilePicker to save the file. Pay attention to the following key points:

Permission Description

Constraints

When using APIs that require the SystemCapability.FileManagement.UserFileService.FolderSelection system capability, you can call canIUse to check whether the device has this system capability:

if (!canIUse('SystemCapability.FileManagement.UserFileService.FolderSelection')) {
      console.error('This API is not supported on this device');
      return;
}

System Isolation Description

Saving Images or Videos

PhotoViewPicker will not be maintained in later versions. You are advised to use Media Library Kit to save media assets.

If the security component cannot be called to save images and videos in your development, use PhotoAccessHelper.showAssetsCreationDialog to save images and videos.

Saving Documents

  1. Import modules.

    import { picker, fileIo } from '@kit.CoreFileKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { common } from '@kit.AbilityKit';
  2. Configure the file saving options DocumentSaveOptions based on your actual service requirements. The following code only lists the configuration reference for each option.

    // Create a documentSaveOptions instance.
    const documentSaveOptions = new picker.DocumentSaveOptions();
    // (Optional) Name of the file to save. The default value is empty.
    documentSaveOptions.newFileNames = ["DocumentViewPicker01.txt"];
    // Specify the URI of the file or directory to save (optional).
    documentSaveOptions.defaultFilePathUri = "file://docs/storage/Users/currentUser/test";
    // File type. The value is in ['Suffix type description|Suffix type'] format. To save all files, use 'All files (*.*)|.*' (optional). If multiple suffixes are selected (a maximum of 100 suffixes can be filtered), the first suffix is selected by default. If this parameter is not transferred, no suffix is filtered by default.
    documentSaveOptions.fileSuffixChoices = ['Document|.txt', '.pdf'];
    // When saving files, the application determines whether to pre-create empty files. The default value is true, where the Picker pre-creates empty files and returns an array of file URIs. When the value is set to false, no empty files are pre-created, and only an array of file URIs is returned.
    documentSaveOptions.autoCreateEmptyFile = false;
  3. Create a DocumentViewPicker instance, and call save() to open the FilePicker interface for file saving.

    let uris: string[] = [];
    let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
    const documentViewPicker = new picker.DocumentViewPicker(context);
    documentViewPicker.save(documentSaveOptions).then((documentSaveResult: string[]) => {
      uris = documentSaveResult;
      console.info('documentViewPicker.save to file succeed and uris are:' + uris);
      // ···
    }).catch((err: BusinessError) => {
      console.error(`Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
    });
    • By default, Picker presets an empty file and returns the URI array of the saved file. After obtaining the URI, the app can use the APIs provided by the file management module to read and write the file.
    • Avoid directly operating the URI in the Picker callback.
    • It is recommended that you use a global variable to store the URI for subsequent use.
    • You can go directly to the download directory through DOWNLOAD mode.
  4. After the UI returns from FilePicker, use fileIo.openSync to open the file through the URI and obtain the file descriptor (FD).

    if (uris.length > 0) {
       let uri: string = uris[0];
       // Note that the interface permission parameter is fileIo.OpenMode.READ_WRITE.
       let file = fileIo.openSync(uri, fileIo.OpenMode.READ_WRITE);
       console.info('file fd: ' + file.fd);
    }
  5. Use fileIo.writeSync to edit and modify the file through the FD, and close the FD after the modification is complete.

    let writeLen: number = fileIo.writeSync(file.fd, 'hello, world');
    console.info('write data to file succeed and size is:' + writeLen);
    fileIo.closeSync(file);

Saving Audio Clips

  1. Import modules.

    import { picker, fileIo } from '@kit.CoreFileKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { common } from '@kit.AbilityKit';
  2. Configure the save options.

    const audioSaveOptions = new picker.AudioSaveOptions();
    // (Optional) Name of the document to save.
    audioSaveOptions.newFileNames = ['AudioViewPicker01.mp3'];
  3. Create an AudioViewPicker instance and call save() to start the FilePicker page to save the audio clip.

    let uris: string[] = [];
    let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
    const audioViewPicker = new picker.AudioViewPicker(context);
    audioViewPicker.save(audioSaveOptions).then((audioSelectResult: string[]) => {
      uris = audioSelectResult;
      console.info('audioViewPicker.save to file succeed and uri is:' + uris);
      // ···
    }).catch((err: BusinessError) => {
      console.error(`Invoke audioViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
    });
    • By default, Picker presets an empty file and returns the URI array of the saved file. After obtaining the URI, the app can use the APIs provided by the file management module to read and write the file.
    • Avoid directly operating the URI in the Picker callback.
    • It is recommended that you use a global variable to store the URI for subsequent use.
    • You can go directly to the download directory through DOWNLOAD mode.
  4. After the UI returns from FilePicker, you can use fileIo.openSync to open the file through the URI and obtain the file descriptor (FD).

    if (uris.length > 0) {
       let uri: string = uris[0];
       // Note that the permission specified by the mode parameter of fs.openSync() is fileIo.OpenMode.READ_WRITE.
       let file = fileIo.openSync(uri, fileIo.OpenMode.READ_WRITE);
       console.info('file fd: ' + file.fd);
    }
  5. Use fileIo.writeSync to edit and modify the file through the FD, and close the FD after the modification is complete.

    let writeLen = fileIo.writeSync(file.fd, 'hello, world');
    console.info('write data to file succeed and size is:' + writeLen);
    fileIo.closeSync(file);

Saving Files to the Download Directory

Characteristics

Directories created in DOWNLOAD mode are used only to store files. Directories are not isolated from each other. You are not advised to store sensitive application data.

  1. Import modules.

    import { fileUri, picker, fileIo } from '@kit.CoreFileKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { common } from '@kit.AbilityKit';
  2. Configure the download mode.

    const documentSaveOptions = new picker.DocumentSaveOptions();
    // Set pickerMode to DOWNLOAD, which takes precedence over the settings in documentSaveOptions.
    documentSaveOptions.pickerMode = picker.DocumentPickerMode.DOWNLOAD;
  3. Save the file to the Download directory.

    let uri: string = '';
    // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext.
    let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
    const documentViewPicker = new picker.DocumentViewPicker(context);
    const documentSaveOptions = new picker.DocumentSaveOptions();
    documentSaveOptions.pickerMode = picker.DocumentPickerMode.DOWNLOAD;
    documentViewPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
      uri = documentSaveResult[0];
      console.info('documentViewPicker.save succeed and uri is:' + uri);
      const testFilePath = new fileUri.FileUri(uri + '/test.txt').path;
      const file = fileIo.openSync(testFilePath, fileIo.OpenMode.CREATE | fileIo.OpenMode.READ_WRITE);
      fileIo.writeSync(file.fd, 'Hello World!');
      fileIo.closeSync(file.fd);
    }).catch((err: BusinessError) => {
      console.error(`Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
    })