has.saveImageToPhotosAlbum(Object object)
保存图片到系统相册。调用该接口会触发弹窗,用户确认后保存。
起始版本: 1.0.0
参数:
参数为Object对象,包括以下字段。
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| filePath | string | 是 | 图片文件路径,可以是临时文件路径或永久文件路径(本地路径),不支持网络路径。 |
| success | function | 否 | 接口调用成功的回调函数。 |
| fail | function | 否 | 接口调用失败的回调函数。 |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行)。 |
错误码:
| 错误码 | 错误信息 |
|---|---|
| 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. |
示例:
has.saveImageToPhotosAlbum({
filePath: 'internal://tmp/xxx.png', // 此处仅为样例,请开发者更换为可用图片资源地址。
success: () => {
console.info('saveImageToPhotosAlbum success');
},
fail: (err) => {
console.error('saveImageToPhotosAlbum fail', err);
},
complete: (res) => {
console.info('saveImageToPhotosAlbum complete', res);
}
}); has.previewImage(Object object)
在新页面中预览图片,预览的过程中用户可以进行保存图片等操作。
注意事项: 在调用此接口时如果需要支持保存网络资源,需要先完成配置服务器域名。
起始版本: 1.0.0
参数:
参数为Object对象,包括以下字段。
| 参数 | 类型 | 默认值 | 必填 | 描述 |
|---|---|---|---|---|
| urls | Array<string> | - | 是 | 需要预览的图片列表(支持网络图片和本地图片,本地图片路径以internal://开头)。 |
| showmenu | boolean | true | 否 | 是否显示长按菜单。 |
| current | string | urls的第一个元素(即第一张图片url) | 否 | 当前显示的图片的链接。 |
| success | function | - | 否 | 接口调用成功的回调函数。 |
| fail | function | - | 否 | 接口调用失败的回调函数。 |
| complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行)。 |
错误码:
| 错误码 | 错误信息 |
|---|---|
| 102 | 1. urls exceed max length. 2. urls format is invalid. |
| 70000201 | previewImage error. |
示例:
has.previewImage({
current: 'https://www.example.com/xxx.jpeg', // 此处仅为样例,请开发者更换为可用图片资源地址
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(Object object)
获取图片的信息。
起始版本: 1.0.0
注意事项: 在调用此接口时如果使用网络资源,需要先完成配置服务器域名。
参数:
参数为Object对象,包括以下字段。
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| src | string | 是 | 图片的路径,支持网络路径、本地路径、代码包路径。 |
| success | function | 否 | 接口调用成功的回调函数。 |
| fail | function | 否 | 接口调用失败的回调函数。 |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行)。 |
success返回值:
| 参数 | 类型 | 描述 |
|---|---|---|
| width | number | 图片原始宽度,单位:px。不考虑旋转。 |
| height | number | 图片原始高度,单位:px。不考虑旋转。 |
| path | string | 图片的本地路径。 |
| type | string | 图片格式。 |
| orientation | string | - up:默认方向(手机横持拍照),对应Exif中的1。或无orientation信息。 - up-mirrored:默认方向(手机横持拍照)、镜像翻转,对应Exif中的2。 - down:旋转180度,对应Exif中的3。 - down-mirrored:旋转180度、镜像翻转,对应Exif中的4。 - left-mirrored:逆时针旋转90°、镜像翻转,对应Exif中的5。 - right:顺时针旋转90°,对应Exif中的6。 - right-mirrored:顺时针旋转90°、镜像翻转,对应Exif中的7。 - left:逆时针旋转90°,对应Exif中的8。 起始版本: 1.0.13 依赖关系: HarmonyOS版本 ≥ 5.0.1 |
错误码:
| 错误码 | 错误信息 |
|---|---|
| 101 | applicationContext is undefined. |
| 102 | src is invalid. |
| 70001301 | 1. file is not exist. 2. getImageInfo error. |
示例:
has.getImageInfo({
src: 'https://www.example.com/xxx.png', // 此处仅为样例,请开发者更换为可用图片资源地址
success: (res) => {
console.info('getImageInfo success', res);
},
fail: (err) => {
console.error('getImageInfo fail', err);
},
complete: (res) => {
console.info('getImageInfo complete', res);
}
}); has.chooseImage(Object object)
从本地相册选择图片或使用相机拍照。
起始版本: 1.0.0
约束与限制:
当前接口在元服务被嵌入式运行时暂不支持使用,建议规避或替换类似功能接口。
参数:
参数为Object对象,包括以下字段。
| 参数 | 类型 | 默认值 | 必填 | 描述 |
|---|---|---|---|---|
| count | number | 9 | 否 | 最多可以选择的图片张数。默认值:9,最大不能超过20。 |
| sizeType | Array<string> | ['original', 'compressed'] | 否 | 所选的图片的尺寸。 - original:原图。 - compressed:压缩图。 |
| sourceType | Array<string> | ['album', 'camera'] | 否 | 选择图片的来源。 - album:从相册选图。 - camera:使用相机拍摄。 |
| success | function | - | 否 | 接口调用成功的回调函数。 |
| fail | function | - | 否 | 接口调用失败的回调函数。 |
| complete | function | - | 否 | 接口调用结束的回调函数(调用成功、失败都会执行)。 |
success返回值:
| 参数 | 类型 | 描述 |
|---|---|---|
| tempFilePaths | Array<string> | 图片的本地临时文件路径列表(本地路径)。 |
| tempFiles | Array<Object> | 图片的本地临时文件列表。 |
tempFiles:
| 参数 | 类型 | 描述 |
|---|---|---|
| path | string | 所选图片的临时路径。 |
| size | number | 所选图片的大小,单位:B。 |
错误码:
| 错误码 | 错误信息 |
|---|---|
| 101 | applicationContext is undefined. |
| 102 | count is invalid. |
| 70000102 | 1. chooseImage error. 2. no image selected. |
示例:
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(Object object)
打开文件选择器选择文件。
起始版本: 1.0.20
参数:
参数为Object对象,包括以下字段。
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| count | number | 是 | 最多可以选择的文件个数(1-100)。 |
| type | string | 是 | 所选文件的类型。 - file:文档文件 - audio:音频文件 - image:图片文件 - video:视频文件。 |
| extension | Array<string> | 否 | 根据文件扩展名过滤,仅在type = 'file'时有效,数组中不允许包含空字符串。默认不过滤。 |
| success | function | 否 | 接口调用成功的回调函数。 |
| fail | function | 否 | 接口调用失败的回调函数。 |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行)。 |
success返回值:
| 参数 | 类型 | 描述 |
|---|---|---|
| tempFiles | Array<Object> | 返回所选文件对象的数组。 |
tempFiles:
| 属性 | 类型 | 描述 |
|---|---|---|
| path | string | 所选文件的文件路径。 注意: 此路径为系统媒体库原始路径,应用无法直接读写,如需操作文件,请先调用FileSystemManager.copyFile或FileSystemManager.copyFileSync接口将文件拷贝到沙箱再进行使用。 |
| name | string | 所选文件的文件名称。 |
| size | number | 所选文件的文件大小,单位:B。 |
错误码:
| 错误码 | 错误信息 |
|---|---|
| 102 | type is invalid. |
| 70005701 | 1. chooseFile, invoke failed. 2. chooseFile error. |
| 70005801 | 1. no image selected. 2. chooseFromPhotos error. |
示例:
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(Object object)
拉起图片编辑类应用,由对应的应用完成图片编辑操作,编辑完成后将结果返回给ASCF元服务。
起始版本: 2.0.2
参数:
参数为Object对象,包括以下字段。
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| src | string | 是 | 图片路径,支持本地路径、代码包路径。 |
| success | function | 否 | 接口调用成功的回调函数。 |
| fail | function | 否 | 接口调用失败的回调函数。 |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行)。 |
success返回值:
| 参数 | 类型 | 描述 |
|---|---|---|
| tempFilePath | string | 返回编辑后的图片临时文件路径(本地路径)。 |
错误码:
| 错误码 | 错误信息 |
|---|---|
| 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. |
示例:
has.editImageByThird({
src: 'internal://tmp/xxx.png', // 此处仅为样例,请开发者更换为可用图片资源地址。
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);
}
});