目录

Image

has.saveImageToPhotosAlbum

has.saveImageToPhotosAlbum(Object object)

Saves an image to the system album. When this API is called, a dialog box is displayed to ask the user whether to save the image.

Since: 1.0.0

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
filePath string Yes Image file path, which can be a temporary or permanent local file path. Network paths are not supported.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error codes

Error Code Error Message
70000901 image is saving.
70001201

1. file path is invalid.

2. uiContext is null.

3. no need save image.

4. save image to album error.

5. saveImageToPhotosAlbum error.

Example:

has.saveImageToPhotosAlbum({
  filePath: 'internal://tmp/xxx.png', // Replace the example with the actual image resource address.
  success: () => {
    console.info('saveImageToPhotosAlbum success');
  },
  fail: (err) => {
    console.error('saveImageToPhotosAlbum fail', err);
  },
  complete: (res) => {
    console.info('saveImageToPhotosAlbum complete', res);
  }
});

has.previewImage

has.previewImage(Object object)

Previews an image on the new page. During the preview, users can operate the image such as saving it.

Precautions: To save network resources when calling this API, you need to configure the server domain name first.

Since: 1.0.0

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
urls Array<string> - Yes List of images to preview. Both network and local images are supported. The local image path starts with internal://.
showmenu boolean true No Whether to display the touch and hold menu.
current string The first element of urls (that is, the URL of the first image). No URL of the current image.
success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Error codes

Error Code Error Message
102

1. urls exceed max length.

2. urls format is invalid.

70000201 previewImage error.

Example:

has.previewImage({
  current: 'https://www.example.com/xxx.jpeg', // Replace the example with the actual image resource address.
  urls: ['https://www.example.com/xxx.jpeg',
    'https://www.example.com/xxxx.png',
    'https://www.example.com/xxxxx.jpeg@h_1280'],
  showmenu: true,
  success: () => {
    console.info('previewImage success');
  },
  fail: (err) => {
    console.error('previewImage fail', err);
  },
  complete: (res) => {
    console.info('previewImage complete', res);
  }
});

has.getImageInfo

has.getImageInfo(Object object)

Obtains the image information.

Since: 1.0.0

Precautions: If network resources are required when this API is called, you need to configure the server domain name first.

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
src string Yes Image path, which can be a network path, local path, or code package path.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

Parameter Type Description
width number Original image width, in pixels. Image rotation is not considered.
height number Original image height, in pixels. Image rotation is not considered.
path string Local path of the image.
type string Image format.
orientation string

Device orientation during photographing.

- up: The phone is held horizontally for photo capture. This value corresponds to 1 in Exif. Alternatively, there is no orientation information.

- up-mirrored: The phone is held horizontally for photo capture, and the image is mirrored left-to-right. This value corresponds to 2 in Exif.

- down: The image is upside-down (rotated by 180 degrees). This value corresponds to 3 in Exif.

- down-mirrored: The image is upside-down (rotated by 180 degrees) and is mirrored left-to-right. This value corresponds to 4 in Exif.

- left-mirrored: The image is rotated counterclockwise by 90 degrees and is mirrored left-to-right. This value corresponds to 5 in Exif.

- right: The image is rotated clockwise by 90 degrees. This value corresponds to 6 in Exif.

- right-mirrored: The image is rotated clockwise by 90 degrees and is mirrored left-to-right. This value corresponds to 7 in Exif.

- left: The image is rotated counterclockwise by 90 degrees. This value corresponds to 8 in Exif.

Since: 1.0.13

Dependency: HarmonyOS 5.0.1 or later

Error codes

Error Code Error Message
101 applicationContext is undefined.
102 src is invalid.
70001301

1. file is not exist.

2. getImageInfo error.

Example:

has.getImageInfo({
  src: 'https://www.example.com/xxx.png', // Replace the example with the actual image resource address.
  success: (res) => {
    console.info('getImageInfo success', res);
  },
  fail: (err) => {
    console.error('getImageInfo fail', err);
  },
  complete: (res) => {
    console.info('getImageInfo complete', res);
  }
});

has.chooseImage

has.chooseImage(Object object)

Selects images from the local album or takes photos using the camera.

Since: 1.0.0

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
count number 9 No Maximum number of images that can be selected. The default value is 9, and the maximum value is 20.
sizeType Array<string> ['original', 'compressed'] No

Size of the selected image.

- original: original size.

- compressed: compressed size.

sourceType Array<string> ['album', 'camera'] No

Image source.

- album: selected from the album.

- camera: captured using the camera.

success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

Parameter Type Description
tempFilePaths Array<string> List of temporary file paths in local.
tempFiles Array<Object> List of local temporary image files.

tempFiles

Parameter Type Description
path string Temporary path of the selected image.
size number Size of the selected image, in bytes.

Error codes

Error Code Error Message
101 applicationContext is undefined.
102 count is invalid.
70000102

1. chooseImage error.

2. no image selected.

Example:

has.chooseImage({
  sourceType: ['album', 'camera'],
  count: 9,
  success: (res) => {
    const tempFiles = res.tempFiles;
    console.info('chooseImage success', tempFiles);
  },
  fail: (err) => {
    console.error('chooseImage fail', err);
  },
  complete: (res) => {
    console.info('chooseImage complete', res);
  }
});

has.chooseFile

has.chooseFile(Object object)

Opens the file selector to select a file.

Since: 1.0.20

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
count number Yes Maximum number of files that can be selected. The value ranges from 1 to 100.
type string Yes

Type of the selected file.

- file: document file

- audio: audio file

- image: image file

- video: video file

extension Array<string> No Filtering based on file name extensions. This parameter is valid only when type is set to file. The array cannot contain empty strings. By default, no filtering is performed.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

Parameter Type Description
tempFiles Array<Object> Array of the selected file objects.

tempFiles

Attribute Type Description
path string

File path of the selected file.

NOTE

This path is the original path of the system media library and cannot be directly read or written by apps. To operate files, call the FileSystemManager.copyFile or FileSystemManager.copyFileSync API to copy the files to the sandbox first.

name string Name of the selected file.
size number Size of the selected file, in bytes.

Error codes

Error Code Error Message
102 type is invalid.
70005701

1. chooseFile, invoke failed.

2. chooseFile error.

70005801

1. no image selected.

2. chooseFromPhotos error.

Example:

has.chooseFile({
  type: 'image',
  count: 5,
  success: (res) => {
    const tempFiles = res.tempFiles;
    console.info('chooseFile success', tempFiles);
  },
  fail: (err) => {
    console.error('chooseFile fail', err);
  },
  complete: (res) => {
    console.info('chooseFile complete', res);
  }
});

has.editImageByThird

has.editImageByThird(Object object)

Starts an image editing app. The app edits the image and returns the result to the ASCF atomic service.

Since: 2.0.2

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
src string Yes Image path, which can be a local path or code package path.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

Parameter Type Description
tempFilePath string Temporary file path (local path) of the edited image.

Error codes

Error Code Error Message
101

1. applicationContext is undefined.

2. rawFilePath is invalid.

102 rawFilePath is invalid.
104 getImageUri: fail no such file or directory.
70006401 editImageByThird error.
70006302 getImageUri encode produced empty file.

Example:

has.editImageByThird({
  src: 'internal://tmp/xxx.png', // Replace the example with the actual image resource address.
  success: (res) => {
    const tempFilePath = res.tempFilePath;
    console.info('editImageByThird success', tempFilePath);
  },
  fail: (err) => {
    console.error('editImageByThird fail', err);
  },
  complete: (res) => {
    console.info('editImageByThird complete', res);
  }
});