DEV Community

HarmonyOS
HarmonyOS

Posted on

How to Resolve the Issue That fs APIs Cannot Read Media File Extended Information ?

Read the original article:How to Resolve the Issue That fs APIs Cannot Read Media File Extended Information ?

How to Resolve the Issue That fs APIs Cannot Read Media File Extended Information

Requirement Description

When using PhotoViewPicker.select with temporary authorization to obtain a URI, calling photoAccessHelper.getAssets to get media file information returns empty and prompts that the permission ohos.permission.READ_IMAGEVIDEO is required. In addition, some information cannot be obtained, such as:

  • how to distinguish file type (image or video),
  • video duration,
  • thumbnail, etc.

Background Knowledge

The photoAccessHelper.getAssets API requires the permission ohos.permission.READ_IMAGEVIDEO. Without this permission, the corresponding information cannot be obtained. However, by using fs.openSync, it is possible to indirectly distinguish file types and obtain video duration.

111.png

Implementation Steps

After obtaining the URI, open the file with fs.openSync to get the file descriptor (fd).

let file = fs.openSync(selectUris[0], fs.OpenMode.READ_ONLY);
Enter fullscreen mode Exit fullscreen mode

222.png

Since there is no direct method to determine the file type, you can determine whether it is an image or a video by checking the file extension.

After distinguishing between image and video, process them separately:

getPathExtension(url) {
  const lastDotIndex = url.lastIndexOf('.');
  if (lastDotIndex < 0) {
    return ''; 
  }
  return url.slice(lastDotIndex + 1);
}
Enter fullscreen mode Exit fullscreen mode

Image

Convert the image into a PixelMap, then use getImageInfo and related APIs to obtain image information.

 const imageSource: image.ImageSource = image.createImageSource(file.fd);
 fs.closeSync(file);
 let decodingOptions: image.DecodingOptions = {
 editable: true,
 desiredPixelFormat: 3,
 }
 imageSource.createPixelMap(decodingOptions).then(async (pixelMap1: image.PixelMap) => {

 });
Enter fullscreen mode Exit fullscreen mode

Video

Use AVMetadataExtractor.fetchMetadata to obtain video metadata such as duration.

333.png


Test Results

Requirement provided successfully.


Limitations or Considerations

  • The ohos.permission.READ_IMAGEVIDEO permission is typically reserved for system or file-management applications; most third-party apps cannot request it directly.
  • There is no direct API for identifying file type without permission. Developers must rely on file extensions, which may be unreliable if the extension is missing or incorrect.
  • Working with file descriptors requires explicit resource management (fs.closeSync) to avoid leaks.
  • This workaround only provides limited metadata. Some advanced fields (e.g., thumbnails for videos) may still be inaccessible without system-level permissions.
  • The solution assumes the user has explicitly selected files through PhotoViewPicker; background or automated scanning of media remains restricted.

Related Documents or Links

Written by Bunyamin Eymen Alagoz

Top comments (0)