怎样获取当前app的缓存目录?

怎样获取当前app的缓存目录?

HarmonyOS
2024-07-12 11:10:30
7740浏览
收藏 0
回答 2
回答 2
按赞同
/
按时间
droidzxy

试试这个呢。


import { common } from '@kit.AbilityKit';


@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  private context = getContext(this) as common.UIAbilityContext;

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            let applicationContext = this.context.getApplicationContext();
            let cacheDir = applicationContext.cacheDir;
            this.message = cacheDir;

          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
分享
微博
QQ
微信
回复
2024-07-12 17:01:52
怎么不算呢
import common from '@ohos.app.ability.common';

@Entry
@Component
export struct GetCacheDirectoryView {
  private context = getContext(this) as common.UIAbilityContext;
  @State cachePath: string = '';

  build() {
    Column() {
      Text(this.cachePath)
        .margin({ bottom: 24 })
      Button() {

        Text('获取应用程序缓存目录地址')
      }
      .onClick(() => {

        const applicationContext = this.context.getApplicationContext();
        // 获取应用文件路径
        const cacheDir = applicationContext.cacheDir;
        this.cachePath = cacheDir + '/test.txt';
      })
      .width(300)
      .height(50)
    }
    .justifyContent(FlexAlign.Center)
    .width('100%')
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
分享
微博
QQ
微信
回复
2024-07-12 18:52:19
相关问题
如何获取当前应用程序缓存目录
3071浏览 • 1回复 待解决
怎样获取app自身bundleName?
1558浏览 • 1回复 待解决
HarmonyOS 如何获取当前APP版本号?
1077浏览 • 1回复 待解决
HarmonyOS关于下载到缓存目录问题
1667浏览 • 1回复 待解决
用file api清理缓存目录
1399浏览 • 1回复 待解决
HarmonyOS怎么清除当前应用缓存
2591浏览 • 1回复 待解决
HarmonyOS 清除APP缓存问题
605浏览 • 1回复 待解决
HarmonyOS 清除app缓存
727浏览 • 1回复 待解决
HarmonyOS 清理app缓存方法
722浏览 • 1回复 待解决
HarmonyOS如何获取当前app版本号和code
1709浏览 • 1回复 待解决
HarmonyOS 关于获取缓存与清除缓存
557浏览 • 1回复 待解决