Use Context.cacheDir to obtain the cache directory of the application. Refer to the code snippet below:
import { common } from '@kit.AbilityKit';
@Entry
@Component
export struct GetCacheDirectoryView {
private context = this.getUIContext().getHostContext() as common.UIAbilityContext;
@State cachePath: string = '';
build() {
Column() {
Text(this.cachePath)
.margin({ bottom: 24 })
Button() {
Text('Get the application cache directory address')
}
.onClick(() => {
const applicationContext = this.context.getApplicationContext();
// Get the application file path
const cacheDir = applicationContext.cacheDir;
this.cachePath = cacheDir + '/test.txt';
})
.width(300)
.height(50)
}
.justifyContent(FlexAlign.Center)
.width('100%')
.height('100%')
}
} Reference