DEV Community

SameX
SameX

Posted on

HarmonyOS Next Application File Sharing: Secure and Efficient Data Exchange

This article aims to deeply explore the technical details of file management in the Huawei HarmonyOS Next system (up to API 12 as of now), and is summarized based on actual development practices.
It mainly serves as a carrier for technical sharing and communication. Mistakes and omissions are inevitable. Colleagues are welcome to put forward valuable opinions and questions so that we can make progress together.
This article is original content, and any form of reprint must indicate the source and the original author.

Introduction
In mobile application development, the file sharing function can help users easily share files between different applications, enhancing the interactivity between applications and the user experience. HarmonyOS Next provides two file sharing methods: based on URI and based on FD, and also provides corresponding security control mechanisms to ensure the security and reliability of file sharing.
1. Basic Concepts of File Sharing
File sharing refers to the exchange and sharing of file data between applications by sharing the file URI or file descriptor (FD). HarmonyOS Next supports file sharing methods between applications based on URI and FD.
2. Characteristics of URI and FD Sharing Methods
| Sharing Method | Advantages | Disadvantages |
|---|---|---|
| Based on URI | Convenient to use, easy to implement, supports temporary authorization, and the permission is revoked when the sharing application exits | Can only share a single file, does not support directory sharing |
| Based on FD | Can share a single file, supports directory sharing | After closing the FD of the file, the shared file cannot be opened again, and the usage is relatively complex |
3. Sharing Permission Control and Security Management
To protect user data security, the application needs to obtain the user's authorization before sharing files. Developers need to declare the required file sharing permissions in the application's configuration file module.json5, for example:

"abilities": [
  {
    "name": "EntryAbility",
    "skills": [
      {
        "actions": [
          "ohos.arkui.intent.action.SEND_DATA"
        ],
        "uris": [
          {
            "scheme": "file",
            "host": "*",
            "path": "/storage/*"
          }
        ]
      }
    ]
  }
]
Enter fullscreen mode Exit fullscreen mode

4. Sample Code: Implementation of URI-based File Sharing
The following sample code shows how to use the API provided by HarmonyOS Next to implement URI-based file sharing:
Sharing a File

import { fileUri } from '@kit.CoreFileKit';
import { wantConstant } from '@kit.AbilityKit';
import { UIAbility } from '@kit.AbilityKit';
import { Want } from '@kit.AbilityKit';
export default class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage: WindowStage) {
    // Get the file sandbox path
    let filePath = this.context.filesDir + '/test.txt';
    // Convert the sandbox path to URI
    let uri = fileUri.getUriFromPath(filePath);
    // Create a sharing intent
    let want: Want = {
      flags: wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION | 
             wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION,
      action: 'ohos.arkui.intent.action.SEND_DATA',
      uri: uri,
      type: 'text/plain'
    };
    // Start sharing
    this.context.startAbility(want)
     .then(() => {
        console.log('Share file successfully');
      })
     .catch((err) => {
        console.error('Failed to share file:', err);
      });
  }
}
Enter fullscreen mode Exit fullscreen mode

Receiving a File

import { fileIo as fs } from '@kit.CoreFileKit';
import { Want } from '@kit.AbilityKit';
export default class EntryAbility extends UIAbility {
  onNewWant(want: Want) {
    // Get the URI of the shared file
    let uri = want.uri;
    // Open the file
    let file = fs.openSync(uri, fs.OpenMode.READ_WRITE);
    // Read the file content
    //...
    // Close the file
    fs.closeSync(file);
  }
}
Enter fullscreen mode Exit fullscreen mode

5. Security Control of File Sharing

  • Permission Control: The application needs to obtain the user's authorization before sharing files. Developers need to declare the required file sharing permissions in the application's configuration file.
  • Access Control: The application can only access the authorized files and cannot access the unauthorized files.
  • Data Encryption: For sensitive data, data encryption technologies such as AES and RSA can be used to ensure data security. 6. Conclusion HarmonyOS Next provides two file sharing methods: based on URI and based on FD, and also provides corresponding security control mechanisms to ensure the security and reliability of file sharing. We can choose the appropriate file sharing method according to the application requirements and use the API provided by HarmonyOS Next to implement the file sharing function, providing users with a more convenient data exchange experience. At the same time, we also need to pay attention to the security control of file sharing to ensure the security of user data.

Top comments (0)