以下内容对应OpenHarmony 3.1 Release 其他版本可能出现问题,请注意

3.1 Release中对应用访问data目录做了权限限制,但是提供了方法去访问应用自己的两个目录。

  • cache
  • files

卸载应用会将所有信息删除

import ability_featureAbility from '@ohos.ability.featureAbility';

// 当前应用上下文
let context = ability_featureAbility.getContext();

async getApplicationDir(){
	let cacheDir: string
	let filesDir: string
	try{
		cacheDir = await context.getCacheDir()
		filesDir = await context.getFilesDir()
	}catch(e){
		console.log(e.message)	
	}

	return {
		cacheDir: cacheDir,
		filesDir: filesDir
	}
}

返回的路径信息(只允许使用这个路径去访问):

/data/storage/el2/base/haps/entry/cache /data/storage/el2/base/haps/entry/files

但这个路径并不是真实路径,真实路径信息对应:

/data/app/el2/100/base/com.ohos.myapplication/haps/entry/cache /data/app/el2/100/base/com.ohos.myapplication/haps/entry/files