This article aims to deeply explore the technical details of file management in Huawei's HarmonyOS Next system (as of currently API12) and is summarized based on actual development practices.
Mainly as a carrier for technical sharing and exchange, it is inevitable that there are mistakes and omissions. Colleagues are welcome to put forward valuable opinions and questions for common progress.
This article is original content. Any form of reprint must indicate the source and original author.
Introduction
User file access is an important function in mobile application development. The File Access Framework of HarmonyOS Next provides developers with a convenient and secure way to implement user file access and management. This article will deeply discuss the architecture, functions, permission control, and usage methods of the File Access Framework, and show how to implement file selection, saving, and file management operations through example code.
1. Introduction to File Access Framework
The File Access Framework is a set of user file access and management frameworks provided by HarmonyOS Next. Based on the ExtensionAbility component mechanism, it provides developers with unified interfaces and methods to facilitate developers to access and manage user files. The File Access Framework has the following characteristics:
- Security: Through the permission control mechanism, ensure that applications can only access authorized files and protect user data security.
- Convenience: Provide unified interfaces and methods to simplify the user file access process and improve development efficiency.
- Cross-device: Support cross-device access to user files, making it convenient for users to share files between different devices. 2. File selection and save permission management To protect user data security, applications need to obtain user authorization before accessing user files. Developers need to declare the required file selection and save permissions in the application's configuration file
module.json5
, for example:
"abilities": [
{
"name": "EntryAbility",
"skills": [
{
"actions": [
"ohos.arkui.intent.action.CHOOSE"
],
"uris": [
{
"scheme": "file",
"host": "*",
"path": "/storage/*"
}
]
},
{
"actions": [
"ohos.arkui.intent.action.SAVE"
],
"uris": [
{
"scheme": "file",
"host": "*",
"path": "/storage/*"
}
]
}
]
}
]
3. User file access interface description
The File Access Framework provides the following interfaces for developers to use:
- FileAccessHelper: Provides API interfaces for file access and management, such as obtaining file paths, obtaining file attributes, creating directories, and deleting files.
- FileAccessExtensionAbility: Provides File Access Framework capabilities, including UserFileManager and ExternalFileManager, for managing files on internal cards and external storage devices. 4. Example code: Operations for selecting and saving files The following example code shows how to use the File Access Framework to select images, save documents, and perform file management operations: Select image
import { fileAccess } from '@ohos.fileAccess';
import { Want } from '@ohos.arkui.ability';
export default class EntryAbility extends Ability {
onWindowStageCreate(windowStage: WindowStage) {
const.wantAgent = wantAgent.createWantAgent();
const chooseIntent = {
action: 'ohos.arkui.intent.action.CHOOSE',
entities: ['image/*'],
type: 'image/*'
};
const chooseWant = wantAgent.createWant(chooseIntent);
this.context.startAbility(chooseWant, (result) => {
if (result) {
const uri = chooseWant.response.result;
// Obtain image attributes
fileAccess.getFileInfo(uri, (error, fileInfo) => {
if (error) {
console.error('Failed to get file info:', error);
} else {
console.log('File info:', fileInfo);
}
});
} else {
console.log('Failed to choose image');
}
});
}
}
Save document
import { fileAccess } from '@ohos.fileAccess';
import { Want } from '@ohos.arkui.ability';
export default class EntryAbility extends Ability {
onWindowStageCreate(windowStage: WindowStage) {
const.wantAgent = wantAgent.createWantAgent();
const saveIntent = {
action: 'ohos.arkui.intent.action.SAVE',
type: 'text/plain'
};
const saveWant = wantAgent.createWant(saveIntent);
this.context.startAbility(saveWant, (result) => {
if (result) {
const uri = saveWant.response.result;
// Save document content
fileAccess.saveFile(uri, 'Hello, World!', (error) => {
if (error) {
console.error('Failed to save file:', error);
} else {
console.log('File saved successfully');
}
});
} else {
console.log('Failed to save document');
}
});
}
}
File management
import { fileAccess } from '@ohos.fileAccess';
export default class EntryAbility extends Ability {
onWindowStageCreate(windowStage: WindowStage) {
const filesDir = this.context.filesDir;
// Create directory
fileAccess.createDir(filesDir + '/newDir', (error) => {
if (error) {
console.error('Failed to create directory:', error);
} else {
console.log('Directory created successfully');
}
});
// Delete file
fileAccess.deleteFile(filesDir + '/test.txt', (error) => {
if (error) {
console.error('Failed to delete file:', error);
} else {
console.log('File deleted successfully');
}
});
}
}
5. Advantages of File Access Framework
The advantages of the File Access Framework are mainly reflected in the following aspects:
- Simplify the development process: Provide unified interfaces and methods to simplify the user file access process and improve development efficiency.
- Improve user experience: Through the permission control mechanism, ensure that applications can only access authorized files, protect user data security, and improve user experience.
- Enhance application functions: Support cross-device access to user files, making it convenient for users to share files between different devices and enhancing application functions. Summary The File Access Framework provides us developers with a convenient and secure way to implement user file access and management. Developers can take advantage of the File Access Framework to easily implement functions such as file selection, saving, management, and cross-device access, providing users with a better experience.
Top comments (0)