DEV Community

HarmonyOS
HarmonyOS

Posted on

How to Obtain Project Resource Files (resfile) in HarmonyOS

Read the original article:How to Obtain Project Resource Files (resfile) in HarmonyOS

Context

In HarmonyOS, files placed under the resfile directory are unpacked into your app’s sandbox after installation. You can resolve their on-device location at runtime via the app Context’s resourceDir property and then access them by file path.

Description

  • After the app is installed, resfile resources are unpacked to the app sandbox.
  • The base directory for these unpacked resources is exposed at runtime as context.resourceDir.
  • Concatenate the file name you packaged (e.g., test.txt) to resourceDir to get the absolute path you can open/read.

Solution / Approach

  1. Get the current UI ability context.
  2. Read the resourceDir from that context.
  3. Build a full path to the target file and access it like a normal file on disk.
import { common } from '@kit.AbilityKit';

let context = getContext(this) as common.UIAbilityContext;

let resourceDir = context.resourceDir

let resourceDirTest = resourceDir + '/test.txt';

//At this point, resourceDirTest can get the path of the resfile folder file.

//The sandbox path log is /data/storage/el1/bundle/entry/resources/resfile/test.txt.
Enter fullscreen mode Exit fullscreen mode

Example resolved path (from log):
/data/storage/el1/bundle/entry/resources/resfile/test.txt

Key Takeaways

  • resfile → sandbox: Packaging under resfile results in files being unpacked into the app’s sandbox at install time.

  • Discover at runtime: Use common.UIAbilityContext.resourceDir to locate the unpacked directory.

  • File-like access: Once you have resourceDir, treat resources as regular files via absolute paths.

  • Deterministic structure: Paths follow the pattern /data/storage/el1/bundle/entry/resources/resfile/.

Additional Resources

  • HarmonyOS docs — Resource categories & access (resfile)

Written by Fatih Turan Gundogdu

Top comments (0)