Developing a File Manager Application (for System Applications Only)

OpenHarmony is prebuilt with the FileManager application. You can also develop your own file manager application as required.

How to Develop

For details about the APIs used to develop a file manager, see User File Access and Management.

  1. Apply for permissions required.
    Apply for the ohos.permission.FILE_ACCESS_MANAGER and ohos.permission.GET_BUNDLE_INFO_PRIVILEGED permissions. For details, see Requesting Permissions for system_basic Applications.

    NOTE

    • ohos.permission.FILE_ACCESS_MANAGER allows your application to use the user file access framework APIs.
    • ohos.permission.GET_BUNDLE_INFO_PRIVILEGED allows your application to obtain information about file management server applications supported by the system.
  2. Import dependent modules.

    import fileAccess from '@ohos.file.fileAccess';
    import fileExtensionInfo from '@ohos.file.fileExtensionInfo';
    

    The fileAccess module provides APIs for basic file operations, and the fileExtensionInfo module provides key structs for application development.

  3. Query device information.
    You can obtain attributes of the devices managed by one or all file management servers in the system. You can also filter devices as required.

    In the user file access framework, RootInfo indicates the attribute information of a device. For example, obtain RootInfo of all devices.

    import common from '@ohos.app.ability.common';
    import { BusinessError } from '@ohos.base';
    import { Filter } from '@ohos.file.fs';
    
    // Obtain the application context.
    let context = getContext(this) as common.UIAbilityContext;
    
    // Create a helper object for connecting to all file management servers in the system.
    let fileAccessHelperAllServer: fileAccess.FileAccessHelper;
    function createFileAccessHelper(): void {
      try {    // this.context is the context passed from EntryAbility.
        fileAccessHelperAllServer = fileAccess.createFileAccessHelper(context);
        if (!fileAccessHelperAllServer) {
          console.error("createFileAccessHelper interface returns an undefined object");
        }
      } catch (err) {
          let error: BusinessError = err as BusinessError;
          console.error("createFileAccessHelper failed, errCode:" + error.code + ", errMessage:" + error.message);
      }
    }
    let rootInfos: Array<fileAccess.RootInfo> = [];
    async function getRoots(): Promise<void>{
      let rootIterator: fileAccess.RootIterator;
      let isDone: boolean = false;
      try {
        rootIterator = await fileAccessHelperAllServer.getRoots();
        if (!rootIterator) {
          console.error("getRoots interface returns an undefined object");
          return;
        }
        while (!isDone) {
          let result = rootIterator.next();
          console.info("next result = " + JSON.stringify(result));
          isDone = result.done;
          if (!isDone)
            rootInfos.push(result.value);
        }
      } catch (err) {
        let error: BusinessError = err as BusinessError;
        console.error("getRoots failed, errCode:" + error.code + ", errMessage:" + error.message);
      }
    }
    
  4. View directories.
    In the user file access framework, FileInfo indicates basic information about a file or folder. You can use listfile() to obtain a FileIterator object that traverses all files (folders) of the next level or use scanfile() to obtain a FileIterator object that meets the specified conditions.

    Currently, listfile() and scanfile() can be called by the RootInfo object to traverse the next-level files or filter the entire directory tree. In addition, listfile() and scanfile() can be called by the FileInfo object to traverse the next-level files or filter the specified directories.

    import { BusinessError } from '@ohos.base';
    import { Filter } from '@ohos.file.fs';
    
    // Start from the root directory.
    let rootInfo: Array<fileAccess.RootInfo> = rootInfos[0];
    let fileInfos: Array<fileAccess.FileInfo> = [];
    let isDone: boolean = false;
    let filter: Filter = {suffix : [".txt", ".jpg", ".xlsx"]}; // Set the filter.
    try {  
      let fileIterator: string = rootInfo.listFile();          // Traverse the root directory of rootinfos[0] and return a FileIterator object.
      // let fileIterator = rootInfo.scanFile(filter); // Filter device rootinfos[0] files that meet the specified conditions and return a FileIterator object.
      if (!fileIterator) {
        console.error("listFile interface returns an undefined object");
      }
      while (!isDone) {
        let result: boolean = fileIterator.next();
        console.info("next result = " + JSON.stringify(result));
        isDone = result.done;
        if (!isDone)
          fileInfos.push(result.value);
      }
    } catch (err) {
     let error: BusinessError = err as BusinessError;
      console.error("listFile failed, errCode:" + error.code + ", errMessage:" + error.message);
    }
    
    // Start from the specified directory.
    let fileInfoDir: Array<fileAccess.FileInfo> = fileInfos[0]; // fileInfoDir indicates the information about a directory.
    let subFileInfos: Array<fileAccess.FileInfo> = [];
    let isDone02: boolean = false;
    let filter02: Filter = {suffix : [".txt", ".jpg", ".xlsx"]}; // Set the filter.
    try {
      let fileIterator: string = fileInfoDir.listFile(); // Traverse files in the specified directory and return a FileIterator object.
      // let fileIterator = rootInfo.scanFile(filter02); // Filter the files in the specified directory and return a FileIterator object.
      if (!fileIterator) {
        console.error("listFile interface returns an undefined object");
      }
      while (!isDone02) {
        let result: boolean = fileIterator.next();
        console.info("next result = " + JSON.stringify(result));
        isDone02 = result.done;
        if (!isDone02)
          subFileInfos.push(result.value);
      }
    } catch (err) {
     let error: BusinessError = err as BusinessError;
      console.error("listFile failed, errCode:" + error.code + ", errMessage:" + error.message);
    }
    
  5. Perform operations on files or folders.
    You can integrate APIs of the user file access framework to implement user behaviors, such as deleting, renaming, creating, and moving a file or folder. The following example shows how to create a file. For details about other APIs, see User File Access and Management.

    import { BusinessError } from '@ohos.base';
    
    // The local device is used as an example.
    // Create a file.
    // sourceUri is the URI in fileinfo of the Download directory.
    // You need to use the obtained URI for development.
    async function creatFile(): Promise<void> {
      let sourceUri: string = "file://docs/storage/Users/currentUser/Download";
      let displayName: string = "file1";
      let fileUri: string = '';
      try {
        // Obtain fileAccessHelperAllServer by referring to the sample code of fileAccess.createFileAccessHelper.
        fileUri = await fileAccessHelperAllServer.createFile(sourceUri, displayName);
        if (!fileUri) {
          console.error("createFile return undefined object");
        }
        console.info("createFile sucess, fileUri: " + JSON.stringify(fileUri));
      } catch (err) {
       let error: BusinessError = err as BusinessError;
       console.error("createFile failed, errCode:" + error.code + ", errMessage:" + error.message);
      };
    }
    

Listening for Device Online/Offline Status

For details about the APIs, see User File Access and Management.

The notify interface can be used to observe not only the changes of directories, but also the device online/offline status.

  1. Apply for permissions required.

    Apply for the ohos.permission.FILE_ACCESS_MANAGER and ohos.permission.GET_BUNDLE_INFO_PRIVILEGED permissions. For details, see Declaring Permissions.

    NOTE

    • ohos.permission.FILE_ACCESS_MANAGER allows your application to use the user file access framework APIs.
    • ohos.permission.GET_BUNDLE_INFO_PRIVILEGED allows your application to obtain information about file management server applications supported by the system.
  2. Import dependent modules.

    import fileAccess from '@ohos.file.fileAccess';
    import fileExtensionInfo from '@ohos.file.fileExtensionInfo';
    

    The fileAccess module provides APIs for basic file operations, and the fileExtensionInfo module provides key structs for application development.

  3. Define an observer callback.

    const callbackDir1 = (NotifyMessageDir: fileAccess.NotifyMessage) => {
      if (NotifyMessageDir != undefined) {
        console.log('NotifyType: ' + NotifyMessageDir.type + 'NotifyUri:' + NotifyMessageDir.uri[0]);
      } else {
       console.error("NotifyMessageDir is undefined");
      }
    }
    
  4. Subscribe to the device online/offline status.

    Pass in the constant DEVICES_URI to the registerObserver method to listen for the device online/offline status.

     import { BusinessError } from '@ohos.base';
       async function UnregisterObserver03() {
         try {
           // Listen for the device online/offline status.
           fileAccessHelper.registerObserver(fileAccess.DEVICES_URI, true, callbackDir1);
         } catch (err) {
           let error: BusinessError = err as BusinessError;
           console.error("unregisterObserver failed, errCode:" + error.code + ", errMessage:" + error.message);
         }
       }
    
  5. Unsubscribe from the device online/offline status.

    Pass in the constant DEVICES_URI to the unregisterObserver method to unsubscribe from the device online/offline status.

    import { BusinessError } from '@ohos.base';
      try {
        // Unsubscribe from the device online/offline status.
        fileAccessHelper.unregisterObserver(fileAccess.DEVICES_URI, callbackDir1);
      } catch (err) {
        let error: BusinessError = err as BusinessError;
        console.error("unregisterObserver failed, errCode:" + error.code + ", errMessage:" + error.message);
      }