Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
aa635c25
D
Docs
项目概览
OpenHarmony
/
Docs
大约 2 年 前同步成功
通知
161
Star
293
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
aa635c25
编写于
7月 05, 2023
作者:
O
openharmony_ci
提交者:
Gitee
7月 05, 2023
浏览文件
操作
浏览文件
下载
差异文件
!20196 新增PhotoAccessHelper开发指导文档和接口文档
Merge pull request !20196 from 胡伟奇/add_PhotoAccessHelpe
上级
d0a11f37
27e31409
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
4924 addition
and
0 deletion
+4924
-0
zh-cn/application-dev/file-management/Readme-CN.md
zh-cn/application-dev/file-management/Readme-CN.md
+6
-0
zh-cn/application-dev/file-management/photoAccessHelper-notify-guidelines.md
...ev/file-management/photoAccessHelper-notify-guidelines.md
+200
-0
zh-cn/application-dev/file-management/photoAccessHelper-overview.md
...ication-dev/file-management/photoAccessHelper-overview.md
+122
-0
zh-cn/application-dev/file-management/photoAccessHelper-resource-guidelines.md
.../file-management/photoAccessHelper-resource-guidelines.md
+283
-0
zh-cn/application-dev/file-management/photoAccessHelper-systemAlbum-guidelines.md
...le-management/photoAccessHelper-systemAlbum-guidelines.md
+306
-0
zh-cn/application-dev/file-management/photoAccessHelper-userAlbum-guidelines.md
...file-management/photoAccessHelper-userAlbum-guidelines.md
+329
-0
zh-cn/application-dev/reference/apis/js-apis-photoAccessHelper.md
...plication-dev/reference/apis/js-apis-photoAccessHelper.md
+3678
-0
未找到文件。
zh-cn/application-dev/file-management/Readme-CN.md
浏览文件 @
aa635c25
...
@@ -19,6 +19,12 @@
...
@@ -19,6 +19,12 @@
-
选择与保存用户文件(FilePicker)
-
选择与保存用户文件(FilePicker)
-
[
选择用户文件
](
select-user-file.md
)
-
[
选择用户文件
](
select-user-file.md
)
-
[
保存用户文件
](
save-user-file.md
)
-
[
保存用户文件
](
save-user-file.md
)
-
相册管理(photoAccessHelper)
-
[
相册管理模块开发概述
](
photoAccessHelper-overview.md
)
-
[
媒体资源(图片、视频)相关
](
photoAccessHelper-resource-guidelines.md
)
-
[
用户相册相关
](
photoAccessHelper-userAlbum-guidelines.md
)
-
[
系统相册相关
](
photoAccessHelper-systemAlbum-guidelines.md
)
-
[
媒体资源变更通知相关
](
photoAccessHelper-notify-guidelines.md
)
-
[
开发用户文件管理器(仅对系统应用开放)
](
dev-user-file-manager.md
)
-
[
开发用户文件管理器(仅对系统应用开放)
](
dev-user-file-manager.md
)
-
[
管理外置存储设备(仅对系统应用开放)
](
manage-external-storage.md
)
-
[
管理外置存储设备(仅对系统应用开放)
](
manage-external-storage.md
)
-
分布式文件系统
-
分布式文件系统
...
...
zh-cn/application-dev/file-management/photoAccessHelper-notify-guidelines.md
0 → 100644
浏览文件 @
aa635c25
# 媒体资源变更通知相关
photoAccessHelper提供监听媒体资源变更的接口,供开发者对指定媒体资源变更进行监听。
> **说明:**
>
> 在进行功能开发前,请开发者查阅[相册管理模块开发概述](photoAccessHelper-overview.md),了解如何获取相册管理模块实例和如何申请相册管理模块功能开发相关权限。
> 文档中使用到photoAccessHelper的地方默认为使用相册管理模块开发概述中获取的对象,如未添加此段代码报photoAccessHelper未定义的错误请自行添加。
媒体资源变更通知相关接口的异步调用仅支持使用callback方式。以下只列出部分接口使用方式,其他使用方式可以查阅
[
API参考
](
../reference/apis/js-apis-photoAccessHelper.md
)
。
如无特别说明,文档中涉及的待获取的资源均视为已经预置且在数据库中存在相应数据。如出现按照示例代码执行出现获取资源为空的情况请确认文件是否已预置,数据库中是否存在该文件的数据。
## 监听指定URI
通过调用
[
registerChange
](
../reference/apis/js-apis-photoAccessHelper.md#registerchange
)
接口监听指定URI。当被监听对象发生变更时返回监听器回调函数的值。
### 打开对FileAsset的URI的监听
打开对FileAsset的URI的进行监听,当目的FileAsset发生变更时,返回监听器回调函数的值。
**前提条件:**
-
获取相册管理模块photoAccessHelper实例。
-
申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'和'ohos.permission.WRITE_IMAGEVIDEO'。
下面以打开对一张图片的URI的监听,通过将这张图片设置为收藏触发监听回调为例。
**开发步骤:**
1.
[
获取指定媒体资源
](
photoAccessHelper-resource-guidelines.md#获取指定媒体资源
)
。
2.
打开对指定媒体资源URI的监听。
3.
将指定媒体资源设置为收藏。
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
predicates
.
equalTo
(
photoAccessHelper
.
ImageVideoKey
.
DISPLAY_NAME
,
'
test.jpg
'
);
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
try
{
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOptions
);
let
fileAsset
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
getAssets fileAsset.uri :
'
+
fileAsset
.
uri
);
let
onCallback
=
(
changeData
)
=>
{
console
.
info
(
'
onCallback successfully, changData:
'
+
JSON
.
stringify
(
changeData
));
}
phAccessHelper
.
registerChange
(
fileAsset
.
uri
,
false
,
onCallback
);
await
fileAsset
.
favorite
(
true
);
fetchResult
.
close
();
}
catch
(
err
)
{
console
.
error
(
'
onCallback failed with err:
'
+
err
);
}
```
### 打开对Album的URI的监听
打开对Album的URI的进行监听,当目的Album发生变更时,返回监听器回调函数的值。
**前提条件:**
-
获取相册管理模块photoAccessHelper实例。
-
申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'和'ohos.permission.WRITE_IMAGEVIDEO'。
下面以打开对一个用户相册的URI的监听,通过将重名名这个相册触发监听回调为例。
**开发步骤:**
1.
[
获取用户相册
](
photoAccessHelper-userAlbum-guidelines.md#获取用户相册
)
。
2.
打开对指定用户相册URI的监听。
3.
将指定用户相册重名名。
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
albumName
=
photoAccessHelper
.
AlbumKey
.
ALBUM_NAME
;
predicates
.
equalTo
(
albumName
,
'
albumName
'
);
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
try
{
let
fetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
USER
,
photoAccessHelper
.
AlbumSubtype
.
USER_GENERIC
,
fetchOptions
);
let
album
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
getAlbums successfullyfully, albumName:
'
+
album
.
albumUri
);
let
onCallback
=
(
changeData
)
=>
{
console
.
info
(
'
onCallback successfully, changData:
'
+
JSON
.
stringify
(
changeData
));
}
phAccessHelper
.
registerChange
(
album
.
albumUri
,
false
,
onCallback
);
album
.
albumName
=
'
newAlbumName
'
+
Date
.
now
();
await
album
.
commitModify
();
fetchResult
.
close
();
}
catch
(
err
)
{
console
.
error
(
'
onCallback failed with err:
'
+
err
);
}
```
## 模糊监听
通过设置forChildUris值为true来打开模糊监听,URI为相册URI时,forChildUris为true能监听到相册中文件的变化,如果是false只能监听相册本身变化。URI为fileAsset时,forChildUris为true、false没有区别,URI为DefaultChangeUri时,forChildUris必须为true,如果为false将找不到该URI,收不到任何消息。
### 打开对所有FileAsset的URI的监听
打开对所有FileAsset的URI的进行监听,当有FileAsset发生变更时,返回监听器回调函数的值。
**前提条件:**
-
获取相册管理模块photoAccessHelper实例。
-
申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'和'ohos.permission.WRITE_IMAGEVIDEO'。
下面以打开所有FileAsset的URI的监听,通过将一张图片进行设置和取消收藏触发监听回调为例。
**开发步骤:**
1.
打开对所有FileAsset的URI的监听。
2.
[
获取指定媒体资源
](
photoAccessHelper-resource-guidelines.md#获取指定媒体资源
)
。
3.
将指定媒体资源设置为收藏。
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
let
onCallback
=
(
changeData
)
=>
{
console
.
info
(
'
onCallback successfully, changData:
'
+
JSON
.
stringify
(
changeData
));
}
phAccessHelper
.
registerChange
(
photoAccessHelper
.
DefaultChangeUri
.
DEFAULT_PHOTO_URI
,
true
,
onCallback
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
try
{
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOptions
);
let
fileAsset
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
getAssets fileAsset.uri :
'
+
fileAsset
.
uri
);
await
fileAsset
.
favorite
(
true
);
fetchResult
.
close
();
}
catch
(
err
)
{
console
.
error
(
'
onCallback failed with err:
'
+
err
);
}
```
## 关闭对指定URI的监听
关闭指定URI的监听,通过调用
[
unRegisterChange
](
../reference/apis/js-apis-photoAccessHelper.md#unregisterchange
)
接口关闭对指定URI的监听。一个URI可以注册多个监听,存在多个callback监听时,可以取消指定注册的callback的监听;不指定callback时解除该URI的所有监听。
**前提条件:**
-
获取相册管理模块photoAccessHelper实例。
-
申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'和'ohos.permission.WRITE_IMAGEVIDEO'。
下面以一张图片的URI的监听中某一个callback监听进行关闭,通过将这张图片设置为收藏实现关闭的callback监听不再触发监听回调为例。
**开发步骤:**
1.
[
获取指定媒体资源
](
photoAccessHelper-resource-guidelines.md#获取指定媒体资源
)
。
2.
关闭对指定媒体资源URI的监听。
3.
将指定媒体资源设置为收藏。
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
predicates
.
equalTo
(
photoAccessHelper
.
ImageVideoKey
.
DISPLAY_NAME
,
'
test.jpg
'
);
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
try
{
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOptions
);
let
fileAsset
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
getAssets fileAsset.uri :
'
+
fileAsset
.
uri
);
let
onCallback1
=
(
changeData
)
=>
{
console
.
info
(
'
onCallback1, changData:
'
+
JSON
.
stringify
(
changeData
));
}
let
onCallback2
=
(
changeData
)
=>
{
console
.
info
(
'
onCallback2, changData:
'
+
JSON
.
stringify
(
changeData
));
}
phAccessHelper
.
registerChange
(
fileAsset
.
uri
,
false
,
onCallback1
);
phAccessHelper
.
registerChange
(
fileAsset
.
uri
,
false
,
onCallback2
);
phAccessHelper
.
unRegisterChange
(
fileAsset
.
uri
,
onCallback1
);
await
fileAsset
.
favorite
(
true
);
fetchResult
.
close
();
}
catch
(
err
)
{
console
.
error
(
'
onCallback failed with err:
'
+
err
);
}
```
zh-cn/application-dev/file-management/photoAccessHelper-overview.md
0 → 100644
浏览文件 @
aa635c25
# 相册管理模块开发概述
PhotoAccessHelper提供相册管理模块相关能力,包括创建相册以及访问、修改相册中的媒体数据信息等,具体分为:
-
[
媒体资源(图片、视频)相关
](
photoAccessHelper-resource-guidelines.md
)
,包括:
-
获取指定媒体资源
-
获取图片和视频缩略图
-
创建媒体资源
-
重命名媒体资源
-
将媒体资源放入回收站
-
[
用户相册相关
](
photoAccessHelper-userAlbum-guidelines.md
)
,包括:
-
创建用户相册
-
获取用户相册
-
重命名用户相册
-
添加图片和视频到用户相册中
-
获取用户相册中的图片和视频
-
从用户相册中移除图片和视频
-
删除用户相册
-
[
系统相册相关
](
photoAccessHelper-systemAlbum-guidelines.md
)
,包括:
-
收藏夹
-
视频相册
-
截屏和录屏相册
-
[
媒体资源变更通知相关
](
photoAccessHelper-notify-guidelines.md
)
,包括:
-
打开对指定URI的监听
-
关闭指定URI的监听
> **说明:**
> 本开发指导基于API Version 10,仅适用于Stage模型。
应用需要先获取相册管理模块实例,才能访问和修改相册中的媒体数据信息。相册管理模块涉及用户个人数据信息,所以应用需要向用户申请相册管理模块读写操作权限才能保证功能的正常运行。在使用相册管理模块相关接口时如无其他注明则默认在工程代码的pages/index.ets或者其他自创的ets文件中使用。
开发者在使用PhotoAccessHelper进行功能开发前,请先掌握以下内容:
-
[
获取相册管理模块实例
](
#获取相册管理模块实例
)
-
[
申请相册管理模块功能相关权限
](
#申请相册管理模块功能相关权限
)
## 获取相册管理模块实例
应用需要使用应用上下文Context通过接口
[
getPhotoAccessHelper
](
../reference/apis/js-apis-photoAccessHelper.md#photoaccesshelpergetphotoaccesshelper
)
,获取相册管理模块实例,用于访问和修改相册中媒体数据信息(如图片、视频)。
**开发步骤:**
1.
导入photoAccessHelper模块以使用相册管理模块相关接口。
2.
通过getContext获取应用上下文。
3.
获取相册管理模块实例。
```
ts
import
photoAccessHelper
from
'
@ohos.file.photoAccessHelper
'
;
// 此处获取的phAccessHelper实例为全局对象,后续文档中使用到的地方默认为使用此处获取的对象,如未添加此段代码报未定义的错误请自行添加。
const
context
=
getContext
(
this
);
let
phAccessHelper
=
photoAccessHelper
.
getPhotoAccessHelper
(
context
);
```
## 申请相册管理模块功能相关权限
相册管理模块的读写操作需要相应权限,在申请权限前,请保证符合
[
权限使用的基本原则
](
../security/accesstoken-overview.md#权限使用的基本原则
)
。涉及的权限如下表。
| 权限名 | 说明 | 授权方式 |
| ------------------------------ | ------------------------------------------ | ---------- |
| ohos.permission.READ_IMAGEVIDEO | 允许应用读取用户外部存储中的图片和视频媒体文件信息。 | user_grant |
| ohos.permission.WRITE_IMAGEVIDEO | 允许应用读写用户外部存储中的图片和视频媒体文件信息。 | user_grant |
以上权限的授权方式均为user_grant(用户授权),即开发者在module.json5文件中配置对应的权限后,需要使用接口
[
abilityAccessCtrl.requestPermissionsFromUser
](
../reference/apis/js-apis-abilityAccessCtrl.md#requestpermissionsfromuser9
)
去校验当前用户是否已授权。如果是,应用可以直接访问/操作目标对象;否则需要弹框向用户申请授权。
> **说明:**<br/>即使用户曾经授予权限,应用在调用受此权限保护的接口前,也应该先检查是否有权限。不能把之前授予的状态持久化,因为用户在动态授予后还可以通过“设置”取消应用的权限。
**开发步骤:**
1.
在配置文件module.json5中声明权限。在配置文件的“module”标签内,增加“requestPermissions”标签,标签内容请根据实际情况填写。标签说明可参考
[
访问控制(权限)开发指导
](
../security/accesstoken-guidelines.md
)
。
```
json
{
"module"
:
{
"requestPermissions"
:
[
{
"name"
:
"ohos.permission.READ_IMAGEVIDEO"
,
"reason"
:
"Permissions required for photoAccessHelper related operations"
,
"usedScene"
:
{
"abilities"
:
[
"EntryAbility"
],
"when"
:
"always"
}
},
{
"name"
:
"ohos.permission.WRITE_IMAGEVIDEO"
,
"reason"
:
"Permissions required for photoAccessHelper related operations"
,
"usedScene"
:
{
"abilities"
:
[
"EntryAbility"
],
"when"
:
"always"
}
},
]
}
}
```
2.
在Ability.ts中onWindowStageCreate里调用requestPermissionsFromUser进行权限校验,可以选择需要动态申请获取的权限自行添加相应代码。
```
ts
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
import
abilityAccessCtrl
,
{
Permissions
}
from
'
@ohos.abilityAccessCtrl
'
;
export
default
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
let
list
:
Array
<
Permissions
>
=
[
'
ohos.permission.READ_IMAGEVIDEO
'
,
'
ohos.permission.WRITE_IMAGEVIDEO
'
];
let
permissionRequestResult
;
let
atManager
=
abilityAccessCtrl
.
createAtManager
();
atManager
.
requestPermissionsFromUser
(
this
.
context
,
list
,
(
err
,
result
)
=>
{
if
(
err
)
{
console
.
error
(
'
requestPermissionsFromUserError:
'
+
JSON
.
stringify
(
err
));
}
else
{
permissionRequestResult
=
result
;
console
.
info
(
'
permissionRequestResult:
'
+
JSON
.
stringify
(
permissionRequestResult
));
}
});
}
}
```
zh-cn/application-dev/file-management/photoAccessHelper-resource-guidelines.md
0 → 100644
浏览文件 @
aa635c25
# 媒体资源使用指导
应用可以通过photoAccessHelper的接口,对媒体资源(图片、视频)进行相关操作。
> **说明:**
>
> 在进行功能开发前,请开发者查阅[相册管理模块开发概述](photoAccessHelper-overview.md),了解如何获取相册管理模块实例和如何申请相册管理模块功能开发相关权限。
> 文档中使用到photoAccessHelper的地方默认为使用相册管理模块开发概述中获取的对象,如未添加此段代码报photoAccessHelper未定义的错误请自行添加。
为了保证应用的运行效率,大部分PhotoAccessHelper调用都是异步的,对于异步调用的API均提供了callback和Promise两种方式,以下示例均采用Promise函数,更多方式可以查阅
[
API参考
](
../reference/apis/js-apis-photoAccessHelper.md
)
。
## 获取指定媒体资源
开发者可以根据特定的条件查询媒体资源,如指定类型、指定日期、指定相册等。
应用通过调用
[
PhotoAccessHelper.getAssets
](
../reference/apis/js-apis-photoAccessHelper.md#getassets
)
获取媒体资源,并传入
[
FetchOptions
](
../reference/apis/js-apis-photoAccessHelper.md#fetchoptions
)
对象指定检索条件。如无特别说明,文档中涉及的待获取的资源均视为已经预置且在数据库中存在相应数据。如出现按照示例代码执行出现获取资源为空的情况请确认文件是否已预置,数据库中是否存在该文件的数据。
如果只想获取某个位置的对象(如第一个、最后一个、指定索引等),可以通过
[
FetchFileResult
](
../reference/apis/js-apis-photoAccessHelper.md#fetchresult
)
中的接口获取对应位置的媒体资源对象。
**前提条件:**
-
获取相册管理模块photoAccessHelper实例。
-
申请相册管理模块读权限'ohos.permission.READ_IMAGEVIDEO'。
-
导入
[
dataSharePredicates
](
../reference/apis/js-apis-data-dataSharePredicates.md
)
模块。
### 指定媒体文件名获取图片或视频资源
下面以查询文件名为'test.jpg'的图片资源为例。
**开发步骤:**
创建FetchOptions对象指定检索条件为检索文件名为'test.jpg'的图片。
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
predicates
.
equalTo
(
photoAccessHelper
.
PhotoKeys
.
DISPLAY_NAME
,
'
test.jpg
'
);
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
```
调用PhotoAccessHelper.getAssets接口获取图片资源。
```
ts
try
{
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOptions
);
let
fileAsset
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
getAssets fileAsset.displayName :
'
+
fileAsset
.
displayName
);
fetchResult
.
close
();
}
catch
(
err
)
{
console
.
error
(
'
getAssets failed with err:
'
+
err
);
}
```
### 指定URI获取图片或视频资源
下面以查询指定URI为'file://media/Photo/1'为例。
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
predicates
.
equalTo
(
photoAccessHelper
.
PhotoKeys
.
URI
,
'
file://media/Photo/1
'
);
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
```
调用PhotoAccessHelper.getAssets接口获取图片资源。
```
ts
try
{
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOptions
);
let
fileAsset
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
getAssets fileAsset.uri :
'
+
fileAsset
.
uri
);
fetchResult
.
close
();
}
catch
(
err
)
{
console
.
error
(
'
getAssets failed with err:
'
+
err
);
}
```
### 指定文件添加的时间获取图片或视频资源
下面以查询指定添加时间为'2022-06-01'至'2023-06-01'这一年内为例。
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
startTime
=
Date
.
parse
(
new
Date
(
'
2022-06-01
'
).
toString
())
/
1000
;
// 查询起始时间距1970年1月1日的秒数值。
let
endTime
=
Date
.
parse
(
new
Date
(
'
2023-06-01
'
).
toString
())
/
1000
;
// 查询结束时间距1970年1月1日的秒数值。
let
date_added
=
photoAccessHelper
.
PhotoKeys
.
DATE_ADDED
;
predicates
.
between
(
date_added
,
startTime
,
endTime
);
predicates
.
orderByDesc
(
date_added
);
// 查询结果按照降序排序。
let
fetchOptions
=
{
fetchColumns
:
[
date_added
],
// date_added属性不属于默认查询列,需要自行添加。
predicates
:
predicates
};
```
调用PhotoAccessHelper.getAssets接口获取图片资源。
```
ts
try
{
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOptions
);
console
.
info
(
'
getAssets count:
'
+
fetchResult
.
getCount
());
let
fileAsset
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
getAssets fileAsset.displayName :
'
+
fileAsset
.
displayName
);
fetchResult
.
close
();
}
catch
(
err
)
{
console
.
error
(
'
getAssets failed with err:
'
+
err
);
}
```
## 获取图片和视频缩略图
通过接口
[
FileAsset.getThumbnail
](
../reference/apis/js-apis-photoAccessHelper.md#getthumbnail
)
,传入缩略图尺寸,可以获取图片和视频缩略图。缩略图常用于UI界面展示。
**前提条件:**
-
获取相册管理模块photoAccessHelper实例。
-
申请相册管理模块读权限'ohos.permission.READ_IMAGEVIDEO'。
-
导入
[
dataSharePredicates
](
../reference/apis/js-apis-data-dataSharePredicates.md
)
模块。
### 获取某张图片的缩略图
当需要在相册展示图片和视频、编辑预览,应用需要获取某张图片的缩略图。
下面以获取一张图片的缩略图为例,缩略图尺寸为720
*
720。
**开发步骤:**
1.
建立检索条件,用于获取图片资源。
2.
调用PhotoAccessHelper.getAssets接口获取图片资源。
3.
调用
[
FetchResult.getFirstObject
](
../reference/apis/js-apis-photoAccessHelper.md#getfirstobject
)
接口获取第一张图片。
4.
调用getThumbnail获取图片的缩略图的
[
PixelMap
](
../reference/apis/js-apis-image.md#pixelmap7
)
。
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
try
{
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOptions
);
let
fileAsset
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
getAssets fileAsset.displayName :
'
+
fileAsset
.
displayName
);
let
size
=
{
width
:
720
,
height
:
720
};
let
pixelMap
=
await
fileAsset
.
getThumbnail
(
size
);
let
imageInfo
=
await
pixelMap
.
getImageInfo
()
console
.
info
(
'
getThumbnail successful, pixelMap ImageInfo size:
'
+
JSON
.
stringify
(
imageInfo
.
size
));
fetchResult
.
close
();
}
catch
(
err
)
{
console
.
error
(
'
getThumbnail failed with err:
'
+
err
);
}
```
## 创建媒体资源
通过接口
[
createAsset
](
../reference/apis/js-apis-photoAccessHelper.md#createasset
)
创建媒体资源。
**前提条件:**
-
获取相册管理模块photoAccessHelper实例。
-
申请相册管理模块权限'ohos.permission.WRITE_IMAGEVIDEO'。
### 创建图片或视频资源
下面以创建一张图片资源为例。
**开发步骤:**
1.
设置文件名并建立创建选项,用于创建图片资源时设置属性。
2.
调用createAsset接口创建图片资源。
```
ts
try
{
let
displayName
=
'
testPhoto
'
+
Date
.
now
()
+
'
.jpg
'
;
let
createOption
=
{
subType
:
photoAccessHelper
.
PhotoSubtype
.
DEFAULT
};
let
fileAsset
=
await
phAccessHelper
.
createAsset
(
displayName
,
createOption
);
console
.
info
(
'
createAsset successfully, file displayName:
'
+
fileAsset
.
displayName
);
}
catch
(
err
)
{
console
.
error
(
'
createAsset failed, message =
'
,
err
);
}
```
## 重命名媒体资源
重命名修改的是文件的FileAsset.displayName属性,即文件的显示文件名,包含文件后缀。
修改后再通过
[
FileAsset.commitModify
](
../reference/apis/js-apis-photoAccessHelper.md#commitmodify
)
更新到数据库中完成修改。
在重命名文件之前,需要先获取文件对象,可以通过
[
FetchResult
](
../reference/apis/js-apis-photoAccessHelper.md#fetchresult
)
中的接口获取对应位置的文件。
**前提条件:**
-
获取相册管理模块photoAccessHelper实例。
-
申请相册管理模块权限'ohos.permission.WRITE_IMAGEVIDEO'和'ohos.permission.READ_IMAGEVIDEO'。
下面以将获取的图片资源中第一个文件重命名为例。
**开发步骤:**
1.
建立检索条件,用于获取图片资源。
2.
调用getFileAssets接口获取目标图片资源。
3.
调用
[
FetchResult.getFirstObject
](
../reference/apis/js-apis-photoAccessHelper.md#getfirstobject
)
接口获取第一张图片,即要重命名的图片对象。
4.
调用FileAsset.set接口将图片重命名为新的名字。
5.
调用FileAsset.commitModify接口将修改的图片属性更新到数据库中完成修改。
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOptions
=
{
fetchColumns
:
[
'
title
'
],
predicates
:
predicates
};
let
newTitle
=
'
newTestPhoto
'
;
try
{
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOptions
);
let
fileAsset
=
await
fetchResult
.
getFirstObject
();
let
title
=
photoAccessHelper
.
PhotoKeys
.
TITLE
;
let
fileAssetTitle
=
fileAsset
.
get
(
title
);
console
.
info
(
'
getAssets fileAsset.title :
'
+
fileAssetTitle
);
fileAsset
.
set
(
title
,
newTitle
);
await
fileAsset
.
commitModify
();
fetchResult
.
close
();
}
catch
(
err
)
{
console
.
error
(
'
commitModify failed with err:
'
+
err
);
}
```
## 将文件放入回收站
通过
[
deleteAssets
](
../reference/apis/js-apis-photoAccessHelper.md#deleteassets
)
可以将文件放入回收站。
放入回收站的文件将会保存30天,30天后会自动彻底删除。在此期间,应用用户可以通过系统应用“文件管理”或“图库”恢复文件。
**前提条件:**
-
获取相册管理模块photoAccessHelper实例。
-
申请相册管理模块权限'ohos.permission.WRITE_IMAGEVIDEO'和'ohos.permission.READ_IMAGEVIDEO'。
下面以将文件检索结果中第一个文件放入回收站为例。
**开发步骤:**
1.
建立检索条件,用于获取图片资源。
2.
调用PhotoAccessHelper.getFileAssets接口获取目标图片资源。
3.
调用
[
FetchResult.getFirstObject
](
../reference/apis/js-apis-photoAccessHelper.md#getfirstobject
)
接口获取第一张图片,即要放入回收站的图片对象。
4.
调用deleteAssets接口将文件放入回收站。
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
try
{
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOptions
);
let
fileAsset
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
getAssets fileAsset.uri :
'
+
fileAsset
.
uri
);
await
phAccessHelper
.
deleteAssets
([
fileAsset
.
uri
]);
fetchResult
.
close
();
}
catch
(
err
)
{
console
.
error
(
'
deleteAssets failed with err:
'
+
err
);
}
```
zh-cn/application-dev/file-management/photoAccessHelper-systemAlbum-guidelines.md
0 → 100644
浏览文件 @
aa635c25
# 系统相册资源使用指导
photoAccessHelper仅提供开发者对收藏夹、视频相册、截屏和录屏相册进行相关操作。
> **说明:**
>
> 在进行功能开发前,请开发者查阅[相册管理模块开发概述](photoAccessHelper-overview.md),了解如何获取相册管理模块实例和如何申请相册管理模块功能开发相关权限。
> 文档中使用到mgr的地方默认为使用相册管理模块开发概述中获取的对象,如未添加此段代码报mgr未定义的错误请自行添加。
为了保证应用的运行效率,大部分photoAccessHelper调用都是异步的,对于异步调用的API均提供了callback和Promise两种方式,以下示例均采用Promise函数,更多方式可以查阅
[
API参考
](
../reference/apis/js-apis-photoAccessHelper.md
)
。
如无特别说明,文档中涉及的待获取的资源均视为已经预置且在数据库中存在相应数据。如出现按照示例代码执行出现获取资源为空的情况请确认文件是否已预置,数据库中是否存在该文件的数据。
## 收藏夹
收藏夹属于系统相册,对图片或视频设置收藏时会自动将其加入到收藏夹中,取消收藏则会从收藏夹中移除。
### 获取收藏夹对象
通过
[
getAlbums
](
../reference/apis/js-apis-photoAccessHelper.md#getalbums
)
接口获取收藏夹对象。
**前提条件:**
-
获取相册管理模块photoAccessHelper实例。
-
申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'。
**开发步骤:**
1.
设置获取收藏夹的参数为photoAccessHelper.AlbumType.SYSTEM和photoAccessHelper.AlbumSubtype.FAVORITE。
2.
调用getAlbums接口获取收藏夹对象。
```
ts
try
{
let
fetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
SYSTEM
,
photoAccessHelper
.
AlbumSubtype
.
FAVORITE
);
let
album
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
get favorite Album successfully, albumUri:
'
+
album
.
albumUri
);
fetchResult
.
close
();
}
catch
(
err
)
{
console
.
error
(
'
get favorite Album failed with err:
'
+
err
);
}
```
### 收藏图片和视频
通过
[
favorite
](
../reference/apis/js-apis-photoAccessHelper.md#favorite
)
接口将图片或者视频设置收藏。
**前提条件:**
-
获取相册管理模块photoAccessHelper实例。
-
申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'和'ohos.permission.WRITE_IMAGEVIDEO'。
下面将以收藏一张图片为例。
**开发步骤:**
1.
[
获取指定媒体资源
](
photoAccessHelper-resource-guidelines.md#获取指定媒体资源
)
。
2.
isFavorite参数设置为true,表示将会设置为收藏。
3.
调用FileAsset.favorite接口设置收藏。
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
predicates
.
equalTo
(
photoAccessHelper
.
ImageVideoKey
.
DISPLAY_NAME
,
'
test.jpg
'
);
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
try
{
let
photoFetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOptions
);
let
fileAsset
=
await
photoFetchResult
.
getFirstObject
();
console
.
info
(
'
getAssets fileAsset.displayName :
'
+
fileAsset
.
displayName
);
let
isFavorite
=
true
;
await
fileAsset
.
favorite
(
isFavorite
);
}
catch
(
err
)
{
console
.
error
(
'
favorite failed with err:
'
+
err
);
}
```
### 获取收藏夹中的图片和视频
先
[
获取收藏夹对象
](
#获取收藏夹对象
)
。然后调用
[
Album.getAssets
](
../reference/apis/js-apis-photoAccessHelper.md#getassets-2
)
接口获取收藏夹中的资源。
**前提条件:**
-
获取相册管理模块photoAccessHelper实例。
-
申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'。
下面以获取收藏夹中的一张图片为例。
**开发步骤:**
1.
[
获取收藏夹对象
](
#获取收藏夹对象
)
。
2.
建立图片检索条件,用于获取图片。
3.
调用Album.getAssets接口获取图片资源。
4.
调用
[
FetchResult.getFirstObject
](
../reference/apis/js-apis-photoAccessHelper.md#getfirstobject
)
接口获取第一张图片。
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
try
{
let
albumFetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
SYSTEM
,
photoAccessHelper
.
AlbumSubtype
.
FAVORITE
);
let
album
=
await
albumFetchResult
.
getFirstObject
();
console
.
info
(
'
get favorite Album successfully, albumUri:
'
+
album
.
albumUri
);
let
photoFetchResult
=
await
album
.
getAssets
(
fetchOptions
);
let
fileAsset
=
await
photoFetchResult
.
getFirstObject
();
console
.
info
(
'
favorite album getAssets successfully, albumName:
'
+
fileAsset
.
displayName
);
photoFetchResult
.
close
();
albumFetchResult
.
close
();
}
catch
(
err
)
{
console
.
error
(
'
favorite failed with err:
'
+
err
);
}
```
### 取消收藏图片或视频
通过
[
favorite
](
../reference/apis/js-apis-photoAccessHelper.md#favorite
)
接口将图片或者视频取消收藏。
**前提条件:**
-
获取相册管理模块photoAccessHelper实例。
-
申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'和'ohos.permission.WRITE_IMAGEVIDEO'。
下面以将一张图片取消收藏为例。
**开发步骤:**
1.
[
获取收藏夹中的图片和视频
](
#获取收藏夹中的图片和视频
)
。
2.
isFavorite参数设置为false。
3.
调用FileAsset.favorite接口设置收藏。
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
try
{
let
albumFetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
SYSTEM
,
photoAccessHelper
.
AlbumSubtype
.
FAVORITE
);
let
album
=
await
albumFetchResult
.
getFirstObject
();
console
.
info
(
'
get favorite Album successfully, albumUri:
'
+
album
.
albumUri
);
let
photoFetchResult
=
await
album
.
getAssets
(
fetchOptions
);
let
fileAsset
=
await
photoFetchResult
.
getFirstObject
();
console
.
info
(
'
favorite album getAssets successfully, albumName:
'
+
fileAsset
.
displayName
);
let
isFavorite
=
false
;
await
fileAsset
.
favorite
(
isFavorite
);
photoFetchResult
.
close
();
albumFetchResult
.
close
();
}
catch
(
err
)
{
console
.
error
(
'
favorite failed with err:
'
+
err
);
}
```
## 视频相册
视频相册属于系统相册,用户文件中属于视频类型的媒体文件会自动加入到视频相册中。
### 获取视频相册对象
通过
[
getAlbums
](
../reference/apis/js-apis-photoAccessHelper.md#getalbums
)
接口获取视频相册对象。
**前提条件:**
-
获取相册管理模块photoAccessHelper实例。
-
申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'。
**开发步骤:**
1.
设置获取视频相册的参数为photoAccessHelper.AlbumType.SYSTEM和photoAccessHelper.AlbumSubtype.VIDEO。
2.
调用getAlbums接口获取视频相册。
```
ts
try
{
let
fetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
SYSTEM
,
photoAccessHelper
.
AlbumSubtype
.
VIDEO
);
let
album
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
get video Album successfully, albumUri:
'
+
album
.
albumUri
);
fetchResult
.
close
();
}
catch
(
err
)
{
console
.
error
(
'
get video Album failed with err:
'
+
err
);
}
```
### 获取视频相册中的视频
先
[
获取视频相册对象
](
#获取视频相册对象
)
。然后调用
[
Album.getAssets
](
../reference/apis/js-apis-photoAccessHelper.md#getassets-2
)
接口获取视频相册对象中的视频资源。
**前提条件:**
-
获取相册管理模块photoAccessHelper实例。
-
申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'。
下面以获取视频相册中的一个视频为例。
**开发步骤:**
1.
先
[
获取视频相册对象
](
#获取视频相册对象
)
。
2.
建立视频检索条件,用于获取视频。
3.
调用Album.getAssets接口获取视频资源。
4.
调用
[
FetchResult.getFirstObject
](
../reference/apis/js-apis-photoAccessHelper.md#getfirstobject
)
接口获取第一个视频。
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
try
{
let
albumFetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
SYSTEM
,
photoAccessHelper
.
AlbumSubtype
.
VIDEO
);
let
album
=
await
albumFetchResult
.
getFirstObject
();
console
.
info
(
'
get video Album successfully, albumUri:
'
+
album
.
albumUri
);
let
videoFetchResult
=
await
album
.
getAssets
(
fetchOptions
);
let
fileAsset
=
await
videoFetchResult
.
getFirstObject
();
console
.
info
(
'
video album getAssets successfully, albumName:
'
+
fileAsset
.
displayName
);
videoFetchResult
.
close
();
albumFetchResult
.
close
();
}
catch
(
err
)
{
console
.
error
(
'
video failed with err:
'
+
err
);
}
```
## 截屏和录屏相册
截屏和录屏相册属于系统相册,用户文件中属于截屏和录屏的媒体文件会自动加入到截屏和录屏相册中。
### 获取截屏和录屏相册对象
通过
[
getAlbums
](
../reference/apis/js-apis-photoAccessHelper.md#getalbums
)
接口获取截屏和录屏相册。
**前提条件:**
-
获取相册管理模块photoAccessHelper实例。
-
申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'。
**开发步骤:**
1.
设置获取截屏和录屏相册的参数为photoAccessHelper.AlbumType.SYSTEM和photoAccessHelper.AlbumSubtype.SCREENSHOT。
2.
调用getAlbums接口获取截屏和录屏相册。
```
ts
try
{
let
fetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
SYSTEM
,
photoAccessHelper
.
AlbumSubtype
.
SCREENSHOT
);
let
album
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
get screenshot Album successfully, albumUri:
'
+
album
.
albumUri
);
fetchResult
.
close
();
}
catch
(
err
)
{
console
.
error
(
'
get screenshot Album failed with err:
'
+
err
);
}
```
### 获取截屏和录屏相册中的媒体资源
先
[
获取截屏和录屏相册对象
](
#获取截屏和录屏相册对象
)
。然后调用
[
Album.getAssets
](
../reference/apis/js-apis-photoAccessHelper.md#getassets-2
)
接口获取截屏和录屏相册对象中的媒体资源。
**前提条件:**
-
获取相册管理模块photoAccessHelper实例。
-
申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'。
下面以获取截屏和录屏相册中的一个媒体资源为例。
**开发步骤:**
1.
先
[
获取截屏和录屏相册对象
](
#获取截屏和录屏相册对象
)
。
2.
建立检索条件,用于获取媒体资源。
3.
调用Album.getAssets接口获取媒体资源。
4.
调用
[
FetchResult.getFirstObject
](
../reference/apis/js-apis-photoAccessHelper.md#getfirstobject
)
接口获取第一个媒体资源。
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
try
{
let
albumFetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
SYSTEM
,
photoAccessHelper
.
AlbumSubtype
.
SCREENSHOT
);
let
album
=
await
albumFetchResult
.
getFirstObject
();
console
.
info
(
'
get screenshot album successfully, albumUri:
'
+
album
.
albumUri
);
let
screenshotFetchResult
=
await
album
.
getAssets
(
fetchOptions
);
let
fileAsset
=
await
screenshotFetchResult
.
getFirstObject
();
console
.
info
(
'
screenshot album getAssets successfully, albumName:
'
+
fileAsset
.
displayName
);
screenshotFetchResult
.
close
();
albumFetchResult
.
close
();
}
catch
(
err
)
{
console
.
error
(
'
screenshot album failed with err:
'
+
err
);
}
```
zh-cn/application-dev/file-management/photoAccessHelper-userAlbum-guidelines.md
0 → 100644
浏览文件 @
aa635c25
# 用户相册资源使用指导
photoAccessHelper提供用户相册相关的接口,供开发者创建、删除用户相册,往用户相册中添加和删除图片和视频资源等。
> **说明:**
>
> 在进行功能开发前,请开发者查阅[相册管理模块开发概述](photoAccessHelper-overview.md),了解如何获取相册管理模块实例和如何申请相册管理模块功能开发相关权限。
> 文档中使用到photoAccessHelper的地方默认为使用相册管理模块开发概述中获取的对象,如未添加此段代码报photoAccessHelper未定义的错误请自行添加。
为了保证应用的运行效率,大部分PhotoAccessHelper接口调用都是异步的,对于异步调用的API均提供了callback和Promise两种方式,以下示例均采用Promise函数,更多方式可以查阅
[
API参考
](
../reference/apis/js-apis-photoAccessHelper.md
)
。
如无特别说明,文档中涉及的待获取的资源均视为已经预置且在数据库中存在相应数据。如出现按照示例代码执行出现获取资源为空的情况请确认文件是否已预置,数据库中是否存在该文件的数据。
## 创建用户相册
通过
[
createAlbum
](
../reference/apis/js-apis-photoAccessHelper.md#createalbum
)
接口创建用户相册。
待创建的相册名参数规格为:
-
相册名字符串长度为1~255。
-
不允许出现的非法英文字符,包括:
<br>
.
\
/ :
*
? " '
` < > | { } [ ]
- 英文字符大小写不敏感。
- 相册名不允许重名。
**前提条件:**
- 获取相册管理模块photoAccessHelper实例。
- 申请相册管理模块权限'ohos.permission.WRITE_IMAGEVIDEO'。
下面以创建一个用户相册为例。
**开发步骤:**
1. 设置相册名。
2. 调用createAlbum接口创建相册。
```ts
try {
let albumName = 'albumName';
let album = await phAccessHelper.createAlbum(albumName);
console.info('createAlbum successfully, album: ' + album.albumName + ' album uri: ' + album.albumUri);
} catch (err) {
console.error('createAlbum failed with err: ' + err);
}
```
## 获取用户相册
通过[getAlbums](../reference/apis/js-apis-photoAccessHelper.md#getalbums)接口获取用户相册。
**前提条件:**
- 获取相册管理模块photoAccessHelper实例。
- 申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'。
下面以获取一个相册名为'albumName'的用户相册为例。
**开发步骤:**
1. 建立检索条件,用于获取用户相册。
2. 调用getAlbums接口获取用户相册资源。
3. 调用[FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject)接口获取第一个用户相册。
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
let predicates = new dataSharePredicates.DataSharePredicates();
let albumName = photoAccessHelper.AlbumKey.ALBUM_NAME;
predicates.equalTo(albumName, 'albumName');
let fetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
let album = await fetchResult.getFirstObject();
console.info('getAlbums successfully, albumName: ' + album.albumName);
fetchResult.close();
} catch (err) {
console.error('getAlbums failed with err: ' + err);
}
```
## 重命名用户相册
重命名用户相册修改的是相册的Albums.albumName属性。
修改后再通过[Album.commitModify](../reference/apis/js-apis-photoAccessHelper.md#commitmodify-2)更新到数据库中完成修改。
在重命名用户相册之前,需要先获取相册对象,可以通过[FetchResult](../reference/apis/js-apis-photoAccessHelper.md#fetchresult)中的接口获取对应位置的用户相册。
重名名用户相册的相册名同样遵循[创建用户相册](#创建用户相册)的参数规格。
**前提条件:**
- 获取相册管理模块photoAccessHelper实例。
- 申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'和'ohos.permission.WRITE_IMAGEVIDEO'。
下面以将一个相册名为'albumName'的用户相册重命名为例。
**开发步骤:**
1. 建立检索条件,用于获取用户相册。
2. 调用getAlbums接口获取用户相册资源。
3. 调用[FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject)接口获取第一个用户相册。
4. 设置新的相册名。
5. 调用Album.commitModify接口将修改的相册属性更新到数据中完成修改。
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
let predicates = new dataSharePredicates.DataSharePredicates();
let albumName = photoAccessHelper.AlbumKey.ALBUM_NAME;
predicates.equalTo(albumName, 'albumName');
let fetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
let album = await fetchResult.getFirstObject();
console.info('getAlbums successfully, albumName: ' + album.albumName);
album.albumName = 'newAlbumName';
await album.commitModify();
fetchResult.close();
} catch (err) {
console.error('commitModify failed with err: ' + err);
}
```
## 添加图片和视频到用户相册中
先[获取用户相册](#获取用户相册)对象和需要添加到相册中的图片或视频的对象数组,然后调用[Album.addAssets](../reference/apis/js-apis-photoAccessHelper.md#addassets)接口往用户相册中添加图片或视频。
**前提条件:**
- 获取相册管理模块photoAccessHelper实例。
- 申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'和'ohos.permission.WRITE_IMAGEVIDEO'。
下面以将往相册名为'albumName'的用户相册中添加一张图片为例。
**开发步骤:**
1. 建立相册检索条件,用于获取用户相册。
2. 建立图片检索条件,用于获取图片。
3. 调用getAlbums接口获取用户相册资源。
4. 调用[FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject)接口获取第一个用户相册。
5. 调用[PhotoAccessHelper.getAssets](../reference/apis/js-apis-photoAccessHelper.md#getassets)接口获取图片资源。
6. 调用[FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject)接口获取第一张图片。
7. 调用Album.addAssets接口往用户相册中添加图片。
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
let albumPredicates = new dataSharePredicates.DataSharePredicates();
let albumName = photoAccessHelper.AlbumKey.ALBUM_NAME;
albumPredicates.equalTo(albumName, 'albumName');
let albumFetchOptions = {
fetchColumns: [],
predicates: albumPredicates
};
let photoPredicates = new dataSharePredicates.DataSharePredicates();
let photoFetchOptions = {
fetchColumns: [],
predicates: photoPredicates
};
try {
let albumFetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
let album = await albumFetchResult.getFirstObject();
console.info('getAlbums successfully, albumName: ' + album.albumName);
let photoFetchResult = await phAccessHelper.getAssets(photoFetchOptions);
let fileAsset = await photoFetchResult.getFirstObject();
console.info('getAssets successfully, albumName: ' + fileAsset.displayName);
await album.addAssets([fileAsset]);
albumFetchResult.close();
photoFetchResult.close();
} catch (err) {
console.error('addAssets failed with err: ' + err);
}
```
## 获取用户相册中的图片和视频
先[获取用户相册](#获取用户相册)对象,然后调用[Album.getAssets](../reference/apis/js-apis-photoAccessHelper.md#getassets-2)接口获取用户相册中的图片资源。
**前提条件:**
- 获取相册管理模块photoAccessHelper实例。
- 申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'和'ohos.permission.WRITE_IMAGEVIDEO'。
下面以获取相册名为'albumName'的用户相册中的一张图片为例。
**开发步骤:**
1. 建立相册检索条件,用于获取用户相册。
2. 建立图片检索条件,用于获取图片。
3. 调用getAlbums接口获取用户相册资源。
4. 调用[FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject)接口获取第一个用户相册。
5. 调用Album.getAssets接口获取用户相册中的图片资源。
6. 调用[FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject)接口获取第一张图片。
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
let albumPredicates = new dataSharePredicates.DataSharePredicates();
let albumName = photoAccessHelper.AlbumKey.ALBUM_NAME;
albumPredicates.equalTo(albumName, 'albumName');
let albumFetchOptions = {
fetchColumns: [],
predicates: albumPredicates
};
let photoPredicates = new dataSharePredicates.DataSharePredicates();
let photoFetchOptions = {
fetchColumns: [],
predicates: photoPredicates
};
try {
let albumFetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
let album = await albumFetchResult.getFirstObject();
console.info('getAlbums successfully, albumName: ' + album.albumName);
let photoFetchResult = await album.getAssets(photoFetchOptions);
let fileAsset = await photoFetchResult.getFirstObject();
console.info('album getAssets successfully, albumName: ' + fileAsset.displayName);
albumFetchResult.close();
photoFetchResult.close();
} catch (err) {
console.error('album getAssets failed with err: ' + err);
}
```
## 从用户相册中移除图片和视频
先[获取用户相册](#获取用户相册)对象,然后调用[Album.getAssets](../reference/apis/js-apis-photoAccessHelper.md#getassets-2)接口获取用户相册中的资源。
选择其中要移除的图片调用[Album.removeAssets](../reference/apis/js-apis-photoAccessHelper.md#removeassets)接口移除。
**前提条件:**
- 获取相册管理模块photoAccessHelper实例。
- 申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'和'ohos.permission.WRITE_IMAGEVIDEO'。
下面以将往相册名为'albumName'的用户相册中移除一张图片为例。
**开发步骤:**
1. 建立相册检索条件,用于获取用户相册。
2. 建立图片检索条件,用于获取图片。
3. 调用getAlbums接口获取用户相册资源。
4. 调用[FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject)接口获取第一个用户相册。
5. 调用Album.getAssets接口获取图片资源。
6. 调用[FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject)接口获取第一张图片。
7. 调用Album.removeAssets接口从用户相册中移除图片。
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
let albumPredicates = new dataSharePredicates.DataSharePredicates();
let albumName = photoAccessHelper.AlbumKey.ALBUM_NAME;
albumPredicates.equalTo(albumName, 'albumName');
let albumFetchOptions = {
fetchColumns: [],
predicates: albumPredicates
};
let photoPredicates = new dataSharePredicates.DataSharePredicates();
let photoFetchOptions = {
fetchColumns: [],
predicates: photoPredicates
};
try {
let albumFetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
let album = await albumFetchResult.getFirstObject();
console.info('getAlbums successfully, albumName: ' + album.albumName);
let photoFetchResult = await album.getAssets(photoFetchOptions);
let fileAsset = await photoFetchResult.getFirstObject();
console.info('album getAssets successfully, albumName: ' + fileAsset.displayName);
await album.removeAssets([fileAsset]);
albumFetchResult.close();
photoFetchResult.close();
} catch (err) {
console.error('removeAssets failed with err: ' + err);
}
```
## 删除用户相册
先[获取用户相册](#获取用户相册)对象,然后调用[deleteAlbums](../reference/apis/js-apis-photoAccessHelper.md#deletealbums)删除用户相册。
**前提条件:**
- 获取相册管理模块photoAccessHelper实例。
- 申请相册管理模块权限'ohos.permission.READ_IMAGEVIDEO'和'ohos.permission.WRITE_IMAGEVIDEO'。
下面以删除一个相册名为'albumName'的用户相册为例。
**开发步骤:**
1. 建立检索条件,用于获取用户相册。
2. 调用getAlbums接口获取用户相册资源。
3. 调用FetchResult.getFirstObject接口获取第一个用户相册。
4. 调用deleteAlbums接口删除第一个用户相册。
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
let predicates = new dataSharePredicates.DataSharePredicates();
let albumName = photoAccessHelper.AlbumKey.ALBUM_NAME;
predicates.equalTo(albumName, '%albumName%');
let fetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
let album = await fetchResult.getFirstObject();
console.info('getAlbums successfully, albumName: ' + album.albumName);
phAccessHelper.deleteAlbums([album]);
fetchResult.close();
} catch (err) {
console.error('deleteAlbums failed with err: ' + err);
}
``
`
zh-cn/application-dev/reference/apis/js-apis-photoAccessHelper.md
0 → 100644
浏览文件 @
aa635c25
# @ohos.file.photoAccessHelper (相册管理模块)
该模块提供相册管理模块能力,包括创建相册以及访问、修改相册中的媒体数据信息等。
> **说明:**
>
> - 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
## 导入模块
```
ts
import
photoAccessHelper
from
'
@ohos.file.photoAccessHelper
'
;
```
## photoAccessHelper.getPhotoAccessHelper
getPhotoAccessHelper(context: Context): PhotoAccessHelper
获取相册管理模块模块的实例,用于访问和修改相册中的媒体文件。
**模型约束**
: 此接口仅可在Stage模型下使用。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**需要权限**
:ohos.permission.READ_IMAGEVIDEO
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------- | ---- | -------------------------- |
| context |
[
Context
](
js-apis-inner-app-context.md
)
| 是 | 传入Ability实例的Context。 |
**返回值:**
| 类型 | 说明 |
| ----------------------------- | :---- |
|
[
PhotoAccessHelper
](
#photoaccesshelper
)
| 相册管理模块模块的实例。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if parameter is invalid. |
**示例:**
```
ts
//此处获取的phAccessHelper实例为全局对象,后续使用到phAccessHelper的地方默认为使用此处获取的对象,如未添加此段代码报phAccessHelper未定义的错误请自行添加
const
context
=
getContext
(
this
);
let
phAccessHelper
=
photoAccessHelper
.
getPhotoAccessHelper
(
context
);
```
## PhotoAccessHelper
### getAssets
getAssets(options: FetchOptions, callback: AsyncCallback
<
FetchResult
<
PhotoAsset
>>
): void;
获取图片和视频资源,使用callback方式返回结果。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**需要权限**
:ohos.permission.READ_IMAGEVIDEO
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------ | ---- | ------------------------- |
| options |
[
FetchOptions
](
#fetchoptions
)
| 是 | 图片和视频检索选项。 |
| callback | AsyncCallback
<
[FetchResult](#fetchresult)
<
[PhotoAsset](#photoasset)
>>
| 是 | callback返回图片和视频检索结果集。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if type options is not FetchOptions. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
getAssets
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
phAccessHelper
.
getAssets
(
fetchOptions
,
async
(
err
,
fetchResult
)
=>
{
if
(
fetchResult
!=
undefined
)
{
console
.
info
(
'
fetchResult success
'
);
let
photoAsset
=
await
fetchResult
.
getFirstObject
();
if
(
photoAsset
!=
undefined
)
{
console
.
info
(
'
photoAsset.displayName :
'
+
photoAsset
.
displayName
);
}
}
else
{
console
.
error
(
'
fetchResult fail
'
+
err
);
}
});
}
```
### getAssets
getAssets(options: FetchOptions): Promise
<
FetchResult
<
PhotoAsset
>>
;
获取图片和视频资源,使用Promise方式返回结果。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**需要权限**
:ohos.permission.READ_IMAGEVIDEO
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------------------- | ---- | ---------------- |
| options |
[
FetchOptions
](
#fetchoptions
)
| 是 | 图片和视频检索选项。 |
**返回值:**
| 类型 | 说明 |
| --------------------------- | -------------- |
| Promise
<
[FetchResult](#fetchresult)
<
[PhotoAsset](#photoasset)
>>
| Promise对象,返回图片和视频数据结果集。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if type options is not FetchOptions. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
getAssets
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
try
{
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOptions
);
if
(
fetchResult
!=
undefined
)
{
console
.
info
(
'
fetchResult success
'
);
let
photoAsset
=
await
fetchResult
.
getFirstObject
();
if
(
photoAsset
!=
undefined
)
{
console
.
info
(
'
photoAsset.displayName :
'
+
photoAsset
.
displayName
);
}
}
}
catch
(
err
)
{
console
.
error
(
'
getAssets failed, message =
'
,
err
);
}
}
```
### createAsset
createAsset(displayName: string, callback: AsyncCallback
<
PhotoAsset
>
): void;
指定待创建的图片或者视频的文件名,创建图片或视频资源,使用callback方式返回结果。
**系统接口**
:此接口为系统接口。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------ | ---- | ------------------------- |
| displayName | string | 是 | 创建的图片或者视频文件名。 |
| callback | AsyncCallback
<
[PhotoAsset](#photoasset)
>
| 是 | callback返回创建的图片和视频结果。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
和
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if type displayName is not string. |
| 14000001 | if type of displayName is invalid. |
**示例:**
```
ts
async
function
example
()
{
console
.
info
(
'
createAssetDemo
'
);
let
testFileName
=
'
testFile
'
+
Date
.
now
()
+
'
.jpg
'
;
phAccessHelper
.
createAsset
(
testFileName
,
(
err
,
photoAsset
)
=>
{
if
(
photoAsset
!=
undefined
)
{
console
.
info
(
'
createAsset file displayName
'
+
photoAsset
.
displayName
);
console
.
info
(
'
createAsset successfully
'
);
}
else
{
console
.
error
(
'
createAsset failed, message =
'
,
err
);
}
});
}
```
### createAsset
createAsset(displayName: string): Promise
<
PhotoAsset
>
;
指定待创建的图片或者视频的文件名,创建图片或视频资源,使用Promise方式返回结果。
**系统接口**
:此接口为系统接口。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------ | ---- | ------------------------- |
| displayName | string | 是 | 创建的图片或者视频文件名。 |
**返回值:**
| 类型 | 说明 |
| --------------------------- | -------------- |
| Promise
<
[PhotoAsset](#photoasset)
>
| Promise对象,返回创建的图片和视频结果。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if type displayName or albumUri is not string. |
| 14000001 | if type of displayName is invalid. |
**示例:**
```
ts
async
function
example
()
{
console
.
info
(
'
createAssetDemo
'
);
try
{
let
testFileName
=
'
testFile
'
+
Date
.
now
()
+
'
.jpg
'
;
let
photoAsset
=
await
phAccessHelper
.
createAsset
(
testFileName
);
console
.
info
(
'
createAsset file displayName
'
+
photoAsset
.
displayName
);
console
.
info
(
'
createAsset successfully
'
);
}
catch
(
err
)
{
console
.
error
(
'
createAsset failed, message =
'
,
err
);
}
}
```
### createAsset
createAsset(displayName: string, options: PhotoCreateOptions, callback: AsyncCallback
<
PhotoAsset
>
): void;
指定待创建的图片或者视频的文件名和创建选项,创建图片或视频资源,使用callback方式返回结果。
**系统接口**
:此接口为系统接口。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------ | ---- | ------------------------- |
| displayName | string | 是 | 创建的图片或者视频文件名。 |
| options |
[
PhotoCreateOptions
](
#photocreateoptions
)
| 是 | 图片或视频的创建选项。 |
| callback | AsyncCallback
<
[PhotoAsset](#photoasset)
>
| 是 | callback返回创建的图片和视频结果。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
文件管理错误码
](
../errorcodes/errorcode-filemanagement.md
)
和
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if type displayName is not string. |
| 14000001 | if type displayName invalid. |
**示例:**
```
ts
async
function
example
()
{
console
.
info
(
'
createAssetDemo
'
);
let
testFileName
=
'
testFile
'
+
Date
.
now
()
+
'
.jpg
'
;
let
createOption
=
{
subtype
:
photoAccessHelper
.
PhotoSubtype
.
DEFAULT
}
phAccessHelper
.
createAsset
(
testFileName
,
createOption
,
(
err
,
photoAsset
)
=>
{
if
(
photoAsset
!=
undefined
)
{
console
.
info
(
'
createAsset file displayName
'
+
photoAsset
.
displayName
);
console
.
info
(
'
createAsset successfully
'
);
}
else
{
console
.
error
(
'
createAsset failed, message =
'
,
err
);
}
});
}
```
### createAsset
createAsset(displayName: string, options: PhotoCreateOptions): Promise
<
PhotoAsset
>
;
指定待创建的图片或者视频的文件名和创建选项,创建图片或视频资源,使用Promise方式返回结果。
**系统接口**
:此接口为系统接口。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------ | ---- | ------------------------- |
| displayName | string | 是 | 创建的图片或者视频文件名。 |
| options |
[
PhotoCreateOptions
](
#photocreateoptions
)
| 是 | 图片或视频的创建选项。 |
**返回值:**
| 类型 | 说明 |
| --------------------------- | -------------- |
| Promise
<
[PhotoAsset](#photoasset)
>
| Promise对象,返回创建的图片和视频结果。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if type displayName is not string. |
| 14000001 | if type of displayName is invalid. |
**示例:**
```
ts
async
function
example
()
{
console
.
info
(
'
createAssetDemo
'
);
try
{
let
testFileName
=
'
testFile
'
+
Date
.
now
()
+
'
.jpg
'
;
let
createOption
=
{
subtype
:
photoAccessHelper
.
PhotoSubtype
.
DEFAULT
}
let
photoAsset
=
await
phAccessHelper
.
createAsset
(
testFileName
,
createOption
);
console
.
info
(
'
createAsset file displayName
'
+
photoAsset
.
displayName
);
console
.
info
(
'
createAsset successfully
'
);
}
catch
(
err
)
{
console
.
error
(
'
createAsset failed, message =
'
,
err
);
}
}
```
### createAsset
createAsset(photoType: PhotoType, extension: string, options: CreateOptions, callback: AsyncCallback
<
string
>
): void;
指定待创建的文件类型、后缀和创建选项,创建图片或视频资源,使用callback方式返回结果。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------ | ---- | ------------------------- |
| photoType |
[
PhotoType
](
#phototype
)
| 是 | 创建的文件类型,IMAGE或者VIDEO类型。 |
| extension | string | 是 | 文件名后缀参数,例如:'jpg'。 |
| options |
[
CreateOptions
](
#createoptions
)
| 是 | 创建选项,例如{title: 'testPhoto'}。 |
| callback | AsyncCallback
<
string
>
| 是 | callback返回创建的图片和视频的uri。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if type createOption is wrong. |
**示例:**
```
ts
async
function
example
()
{
console
.
info
(
'
createAssetDemo
'
);
let
photoType
=
photoAccessHelper
.
PhotoType
.
IMAGE
;
let
extension
=
'
jpg
'
;
let
options
=
{
title
:
'
testPhoto
'
}
phAccessHelper
.
createAsset
(
photoType
,
extension
,
options
,
(
err
,
photoAsset
)
=>
{
if
(
photoAsset
!=
undefined
)
{
console
.
info
(
'
createAsset file displayName
'
+
photoAsset
.
displayName
);
console
.
info
(
'
createAsset successfully
'
);
}
else
{
console
.
error
(
'
createAsset failed, message =
'
,
err
);
}
});
}
```
### createAsset
createAsset(photoType: PhotoType, extension: string, callback: AsyncCallback
<
string
>
): void;
指定待创建的文件类型和后缀,创建图片或视频资源,使用callback方式返回结果。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------ | ---- | ------------------------- |
| photoType |
[
PhotoType
](
#phototype
)
| 是 | 创建的文件类型,IMAGE或者VIDEO类型。 |
| extension | string | 是 | 文件名后缀参数,例如:'jpg'。 |
| callback | AsyncCallback
<
string
>
| 是 | callback返回创建的图片和视频的uri。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if type createOption is wrong. |
**示例:**
```
ts
async
function
example
()
{
console
.
info
(
'
createAssetDemo
'
);
let
photoType
=
photoAccessHelper
.
PhotoType
.
IMAGE
;
let
extension
=
'
jpg
'
;
phAccessHelper
.
createAsset
(
photoType
,
extension
,
(
err
,
photoAsset
)
=>
{
if
(
photoAsset
!=
undefined
)
{
console
.
info
(
'
createAsset file displayName
'
+
photoAsset
.
displayName
);
console
.
info
(
'
createAsset successfully
'
);
}
else
{
console
.
error
(
'
createAsset failed, message =
'
,
err
);
}
});
}
```
### createAsset
createAsset(photoType: PhotoType, extension: string, options?: CreateOptions): Promise
<
string
>
;
指定待创建的文件类型、后缀和创建选项,创建图片或视频资源,使用Promise方式返回结果。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------ | ---- | ------------------------- |
| photoType |
[
PhotoType
](
#phototype
)
| 是 | 创建的文件类型,IMAGE或者VIDEO类型。 |
| extension | string | 是 | 文件名后缀参数,例如:'jpg'。 |
| options |
[
CreateOptions
](
#createoptions
)
| 否 | 创建选项,例如{title: 'testPhoto'}。 |
**返回值:**
| 类型 | 说明 |
| --------------------------- | -------------- |
| Promise
<
string
>
| Promise对象,返回创建的图片和视频的uri。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if type createOption is wrong. |
**示例:**
```
ts
async
function
example
()
{
console
.
info
(
'
createAssetDemo
'
);
try
{
let
photoType
=
photoAccessHelper
.
PhotoType
.
IMAGE
;
let
extension
=
'
jpg
'
;
let
options
=
{
title
:
'
testPhoto
'
}
let
photoAsset
=
await
phAccessHelper
.
createAsset
(
photoType
,
extension
,
options
);
console
.
info
(
'
createAsset file displayName
'
+
photoAsset
.
displayName
);
console
.
info
(
'
createAsset successfully
'
);
}
catch
(
err
)
{
console
.
error
(
'
createAsset failed, message =
'
,
err
);
}
}
```
### createAlbum
createAlbum(name: string, callback: AsyncCallback
<
Album
>
): void;
创建相册,使用callback方式返回结果。
待创建的相册名参数规格为:
-
相册名字符串长度为1~255。
-
不允许出现的非法英文字符,包括:
<br>
. ..
\
/ :
*
? " '
` < > | { } [ ]
- 英文字符大小写不敏感。
- 相册名不允许重名。
**系统接口**:此接口为系统接口。
**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core
**需要权限**:ohos.permission.WRITE_IMAGEVIDEO
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------ | ---- | ------------------------- |
| name | string | 是 | 待创建相册的相册名。 |
| callback | AsyncCallback<[Album](#album)> | 是 | callback返回创建的相册实例。 |
**错误码:**
接口抛出错误码的详细介绍请参见[通用错误码](../errorcodes/errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if parameter is invalid. |
**示例:**
```ts
async function example() {
console.info('createAlbumDemo');
let albumName = 'newAlbumName' + new Date().getTime();
phAccessHelper.createAlbum(albumName, (err, album) => {
if (err) {
console.error('createAlbumCallback failed with err: ' + err);
return;
}
console.info('createAlbumCallback successfully, album: ' + album.albumName + ' album uri: ' + album.albumUri);
});
}
```
### createAlbum
createAlbum(name: string): Promise<Album>;
创建相册,使用Promise方式返回结果。
待创建的相册名参数规格为:
- 相册名字符串长度为1~255。
- 不允许出现的非法英文字符,包括:<br> . .. \ / : * ? " ' `
<
>
| { } [ ]
-
英文字符大小写不敏感。
-
相册名不允许重名。
**系统接口**
:此接口为系统接口。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------ | ---- | ------------------------- |
| name | string | 是 | 待创建相册的相册名。 |
**返回值:**
| 类型 | 说明 |
| --------------------------- | -------------- |
| Promise
<
[Album](#album)
>
| Promise对象,返回创建的相册实例。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if parameter is invalid. |
**示例:**
```
ts
async
function
example
()
{
console
.
info
(
'
createAlbumDemo
'
);
let
albumName
=
'
newAlbumName
'
+
new
Date
().
getTime
();
phAccessHelper
.
createAlbum
(
albumName
).
then
((
album
)
=>
{
console
.
info
(
'
createAlbumPromise successfully, album:
'
+
album
.
albumName
+
'
album uri:
'
+
album
.
albumUri
);
}).
catch
((
err
)
=>
{
console
.
error
(
'
createAlbumPromise failed with err:
'
+
err
);
});
}
```
### deleteAlbums
deleteAlbums(albums: Array
<
Album
>
, callback: AsyncCallback
<
void
>
): void;
删除相册,使用callback方式返回结果。
删除相册前需先确保相册存在,只能删除用户相册。
**系统接口**
:此接口为系统接口。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------ | ---- | ------------------------- |
| albums | Array
<
[Album](#album)
>
| 是 | 待删除相册的数组。 |
| callback | AsyncCallback
<
void
>
| 是 | callback返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
// 示例代码为删除相册名为newAlbumName的相册。
console
.
info
(
'
deleteAlbumsDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
predicates
.
equalTo
(
'
album_name
'
,
'
newAlbumName
'
);
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
USER
,
photoAccessHelper
.
AlbumSubtype
.
USER_GENERIC
,
fetchOptions
);
let
album
=
await
fetchResult
.
getFirstObject
();
phAccessHelper
.
deleteAlbums
([
album
],
(
err
)
=>
{
if
(
err
)
{
console
.
error
(
'
deletePhotoAlbumsCallback failed with err:
'
+
err
);
return
;
}
console
.
info
(
'
deletePhotoAlbumsCallback successfully
'
);
});
fetchResult
.
close
();
}
```
### deleteAlbums
deleteAlbums(albums: Array
<
Album
>
): Promise
<
void
>
;
删除相册,使用Promise方式返回结果。
删除相册前需先确保相册存在,只能删除用户相册。
**系统接口**
:此接口为系统接口。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------ | ---- | ------------------------- |
| albums | Array
<
[Album](#album)
>
| 是 | 待删除相册的数组。 |
**返回值:**
| 类型 | 说明 |
| --------------------------- | -------------- |
| Promise
<
void
>
| Promise对象,返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
// 示例代码为删除相册名为newAlbumName的相册。
console
.
info
(
'
deleteAlbumsDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
predicates
.
equalTo
(
'
album_name
'
,
'
newAlbumName
'
);
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
USER
,
photoAccessHelper
.
AlbumSubtype
.
USER_GENERIC
,
fetchOptions
);
let
album
=
await
fetchResult
.
getFirstObject
();
phAccessHelper
.
deleteAlbums
([
album
]).
then
(()
=>
{
console
.
info
(
'
deletePhotoAlbumsPromise successfully
'
);
}).
catch
((
err
)
=>
{
console
.
error
(
'
deletePhotoAlbumsPromise failed with err:
'
+
err
);
});
fetchResult
.
close
();
}
```
### getAlbums
getAlbums(type: AlbumType, subtype: AlbumSubtype, options: FetchOptions, callback: AsyncCallback
<
FetchResult
<
Album
>>
): void;
根据检索选项和相册类型获取相册,使用callback方式返回结果。
获取相册前需先保证相册存在。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**需要权限**
:ohos.permission.READ_IMAGEVIDEO
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------ | ---- | ------------------------- |
| type |
[
AlbumType
](
#albumtype
)
| 是 | 相册类型。 |
| subtype |
[
AlbumSubtype
](
#albumsubtype
)
| 是 | 相册子类型。 |
| options |
[
FetchOptions
](
#fetchoptions
)
| 是 | 检索选项。 |
| callback | AsyncCallback
<
[FetchResult](#fetchresult)
<
[Album](#album)
>>
| 是 | callback返回获取相册的结果集。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if type options is not FetchOption. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
// 示例代码中为获取相册名为newAlbumName的相册。
console
.
info
(
'
getAlbumsDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
predicates
.
equalTo
(
'
album_name
'
,
'
newAlbumName
'
);
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
USER
,
photoAccessHelper
.
AlbumSubtype
.
USER_GENERIC
,
fetchOptions
,
async
(
err
,
fetchResult
)
=>
{
if
(
err
)
{
console
.
error
(
'
getAlbumsCallback failed with err:
'
+
err
);
return
;
}
if
(
fetchResult
==
undefined
)
{
console
.
error
(
'
getAlbumsCallback fetchResult is undefined
'
);
return
;
}
let
album
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
getAlbumsCallback successfully, albumName:
'
+
album
.
albumName
);
fetchResult
.
close
();
});
}
```
### getAlbums
getAlbums(type: AlbumType, subtype: AlbumSubtype, callback: AsyncCallback
<
FetchResult
<
Album
>>
): void;
根据相册类型获取相册,使用callback方式返回结果。
获取相册前需先保证相册存在。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**需要权限**
:ohos.permission.READ_IMAGEVIDEO
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------ | ---- | ------------------------- |
| type |
[
AlbumType
](
#albumtype
)
| 是 | 相册类型。 |
| subtype |
[
AlbumSubtype
](
#albumsubtype
)
| 是 | 相册子类型。 |
| callback | AsyncCallback
<
[FetchResult](#fetchresult)
<
[Album](#album)
>>
| 是 | callback返回获取相册的结果集。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if type options is not FetchOption. |
**示例:**
```
ts
async
function
example
()
{
// 示例代码中为获取统相册VIDEO,默认已预置。
console
.
info
(
'
getAlbumsDemo
'
);
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
SYSTEM
,
photoAccessHelper
.
AlbumSubtype
.
VIDEO
,
async
(
err
,
fetchResult
)
=>
{
if
(
err
)
{
console
.
error
(
'
getAlbumsCallback failed with err:
'
+
err
);
return
;
}
if
(
fetchResult
==
undefined
)
{
console
.
error
(
'
getAlbumsCallback fetchResult is undefined
'
);
return
;
}
let
album
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
getAlbumsCallback successfully, albumUri:
'
+
album
.
albumUri
);
fetchResult
.
close
();
});
}
```
### getAlbums
getAlbums(type: AlbumType, subtype: AlbumSubtype, options?: FetchOptions): Promise
<
FetchResult
<
Album
>>
;
根据检索选项和相册类型获取相册,使用Promise方式返回结果。
获取相册前需先保证相册存在。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**需要权限**
:ohos.permission.READ_IMAGEVIDEO
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------ | ---- | ------------------------- |
| type |
[
AlbumType
](
#albumtype
)
| 是 | 相册类型。 |
| subtype |
[
AlbumSubtype
](
#albumsubtype
)
| 是 | 相册子类型。 |
| options |
[
FetchOptions
](
#fetchoptions
)
| 否 | 检索选项,不填时默认根据相册类型检索。 |
**返回值:**
| 类型 | 说明 |
| --------------------------- | -------------- |
| Promise
<
[FetchResult](#fetchresult)
<
[Album](#album)
>>
| Promise对象,返回获取相册的结果集。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if type options is not FetchOption. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
// 示例代码中为获取相册名为newAlbumName的相册。
console
.
info
(
'
getAlbumsDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
predicates
.
equalTo
(
'
album_name
'
,
'
newAlbumName
'
);
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
USER
,
photoAccessHelper
.
AlbumSubtype
.
USER_GENERIC
,
fetchOptions
).
then
(
async
(
fetchResult
)
=>
{
if
(
fetchResult
==
undefined
)
{
console
.
error
(
'
getAlbumsPromise fetchResult is undefined
'
);
return
;
}
let
album
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
getAlbumsPromise successfully, albumName:
'
+
album
.
albumName
);
fetchResult
.
close
();
}).
catch
((
err
)
=>
{
console
.
error
(
'
getAlbumsPromise failed with err:
'
+
err
);
});
}
```
### deleteAssets
deleteAssets(uriList: Array
<
string
>
, callback: AsyncCallback
<
void
>
): void;
删除媒体文件,删除的文件进入到回收站,使用callback方式返回结果。
**系统接口**
:此接口为系统接口。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------- |
| uriList | Array
<
string
>
| 是 | 待删除的媒体文件uri数组。 |
| callback | AsyncCallback
<
void
>
| 是 | callback返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
deleteAssetDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
try
{
const
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOptions
);
var
asset
=
await
fetchResult
.
getFirstObject
();
}
catch
(
err
)
{
console
.
info
(
'
fetch failed, message =
'
,
err
);
}
if
(
asset
==
undefined
)
{
console
.
error
(
'
asset not exist
'
);
return
;
}
phAccessHelper
.
deleteAssets
([
asset
.
uri
],
(
err
)
=>
{
if
(
err
==
undefined
)
{
console
.
info
(
'
deleteAssets successfully
'
);
}
else
{
console
.
error
(
'
deleteAssets failed with error:
'
+
err
);
}
});
}
```
### deleteAssets
deleteAssets(uriList: Array
<
string
>
): Promise
<
void
>
;
删除媒体文件,删除的文件进入到回收站,使用Promise方式返回结果。
**系统接口**
:此接口为系统接口。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------- |
| uriList | Array
<
string
>
| 是 | 待删除的媒体文件uri数组。 |
**返回值:**
| 类型 | 说明 |
| --------------------------------------- | ----------------- |
| Promise
<
void
>
| Promise对象,返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
deleteDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
try
{
const
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOptions
);
var
asset
=
await
fetchResult
.
getFirstObject
();
}
catch
(
err
)
{
console
.
info
(
'
fetch failed, message =
'
,
err
);
}
if
(
asset
==
undefined
)
{
console
.
error
(
'
asset not exist
'
);
return
;
}
try
{
await
phAccessHelper
.
deleteAssets
([
asset
.
uri
]);
console
.
info
(
'
deleteAssets successfully
'
);
}
catch
(
err
)
{
console
.
error
(
'
deleteAssets failed with error:
'
+
err
);
}
}
```
### registerChange
registerChange(uri: string, forChildUris: boolean, callback: Callback
<
ChangeData
>
) : void
打开对指定uri的监听,使用callback方式返回异步结果。
**需要权限**
:ohos.permission.READ_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | ------------------------------------------- | ---- | ------------------------------------------------------------ |
| uri | string | 是 | PhotoAsset的uri, Album的uri或
[
DefaultChangeUri
](
#defaultchangeuri
)
的值。 |
| forChildUris | boolean | 是 | 是否模糊监听,uri为相册uri时,forChildUris为true能监听到相册中文件的变化,如果是false只能监听相册本身变化。uri为photoAsset时,forChildUris为true、false没有区别,uri为DefaultChangeUri时,forChildUris必须为true,如果为false将找不到该uri,收不到任何消息。 |
| callback | Callback
<
[ChangeData](#changedata)
>
| 是 | 返回要监听的
[
ChangeData
](
#changedata
)
。注:uri可以注册多个不同的callback监听,
[
unRegisterChange
](
#unregisterchange
)
可以关闭该uri所有监听,也可以关闭指定callback的监听。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
registerChangeDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOptions
);
let
photoAsset
=
await
fetchResult
.
getFirstObject
();
if
(
photoAsset
!=
undefined
)
{
console
.
info
(
'
photoAsset.displayName :
'
+
photoAsset
.
displayName
);
}
let
onCallback1
=
(
changeData
)
=>
{
console
.
info
(
'
onCallback1 success, changData:
'
+
JSON
.
stringify
(
changeData
));
//file had changed, do something
}
let
onCallback2
=
(
changeData
)
=>
{
console
.
info
(
'
onCallback2 success, changData:
'
+
JSON
.
stringify
(
changeData
));
//file had changed, do something
}
// 注册onCallback1监听
phAccessHelper
.
registerChange
(
photoAsset
.
uri
,
false
,
onCallback1
);
// 注册onCallback2监听
phAccessHelper
.
registerChange
(
photoAsset
.
uri
,
false
,
onCallback2
);
photoAsset
.
favorite
(
true
,
(
err
)
=>
{
if
(
err
==
undefined
)
{
console
.
info
(
'
favorite successfully
'
);
}
else
{
console
.
error
(
'
favorite failed with error:
'
+
err
);
}
});
}
```
### unRegisterChange
unRegisterChange(uri: string, callback?: Callback
<
ChangeData
>
): void
关闭指定uri的监听,一个uri可以注册多个监听,存在多个callback监听时,可以取消指定注册的callback的监听;不指定callback时解除该uri的所有监听。
**需要权限**
:ohos.permission.READ_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ |
| uri | string | 是 | PhotoAsset的uri, Album的uri或
[
DefaultChangeUri
](
#defaultchangeuri
)
的值。 |
| callback | Callback
<
[ChangeData](#changedata)
>
| 否 | 解除
[
registerChange
](
#registerchange
)
注册时的callback的监听,不填时,解除该uri的所有监听。注:off指定注册的callback后不会进入此回调。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
offDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOptions
);
let
photoAsset
=
await
fetchResult
.
getFirstObject
();
if
(
photoAsset
!=
undefined
)
{
console
.
info
(
'
photoAsset.displayName :
'
+
photoAsset
.
displayName
);
}
let
onCallback1
=
(
changeData
)
=>
{
console
.
info
(
'
onCallback1 on
'
);
}
let
onCallback2
=
(
changeData
)
=>
{
console
.
info
(
'
onCallback2 on
'
);
}
// 注册onCallback1监听
phAccessHelper
.
registerChange
(
photoAsset
.
uri
,
false
,
onCallback1
);
// 注册onCallback2监听
phAccessHelper
.
registerChange
(
photoAsset
.
uri
,
false
,
onCallback2
);
// 关闭onCallback1监听,onCallback2 继续监听
phAccessHelper
.
unRegisterChange
(
photoAsset
.
uri
,
onCallback1
);
photoAsset
.
favorite
(
true
,
(
err
)
=>
{
if
(
err
==
undefined
)
{
console
.
info
(
'
favorite successfully
'
);
}
else
{
console
.
error
(
'
favorite failed with error:
'
+
err
);
}
});
}
```
### createDeleteRequest
createDeleteRequest(uriList: Array
<
string
>
, callback: AsyncCallback
<
void
>
): void;
创建一个弹出框来删除照片,删除的文件进入到回收站,使用callback方式返回结果。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------- |
| uriList | Array
<
string
>
| 是 | 待删除的媒体文件uri数组。 |
| callback | AsyncCallback
<
void
>
| 是 | callback返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
createDeleteRequestDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
try
{
const
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOptions
);
var
asset
=
await
fetchResult
.
getFirstObject
();
}
catch
(
err
)
{
console
.
info
(
'
fetch failed, message =
'
,
err
);
}
if
(
asset
==
undefined
)
{
console
.
error
(
'
asset not exist
'
);
return
;
}
phAccessHelper
.
createDeleteRequest
([
asset
.
uri
],
(
err
)
=>
{
if
(
err
==
undefined
)
{
console
.
info
(
'
createDeleteRequest successfully
'
);
}
else
{
console
.
error
(
'
createDeleteRequest failed with error:
'
+
err
);
}
});
}
```
### createDeleteRequest
createDeleteRequest(uriList: Array
<
string
>
): Promise
<
void
>
;
创建一个弹出框来删除照片,删除的文件进入到回收站,使用Promise方式返回结果。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------- |
| uriList | Array
<
string
>
| 是 | 待删除的媒体文件uri数组。 |
**返回值:**
| 类型 | 说明 |
| --------------------------------------- | ----------------- |
| Promise
<
void
>
| Promise对象,返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
createDeleteRequestDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOptions
=
{
fetchColumns
:
[],
predicates
:
predicates
};
try
{
const
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOptions
);
var
asset
=
await
fetchResult
.
getFirstObject
();
}
catch
(
err
)
{
console
.
info
(
'
fetch failed, message =
'
,
err
);
}
if
(
asset
==
undefined
)
{
console
.
error
(
'
asset not exist
'
);
return
;
}
try
{
await
phAccessHelper
.
createDeleteRequest
([
asset
.
uri
]);
console
.
info
(
'
createDeleteRequest successfully
'
);
}
catch
(
err
)
{
console
.
error
(
'
createDeleteRequest failed with error:
'
+
err
);
}
}
```
### release
release(callback: AsyncCallback
<
void
>
): void
释放PhotoAccessHelper实例。
当后续不需要使用PhotoAccessHelper实例中的方法时调用。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | -------------------- |
| callback | AsyncCallback
<
void
>
| 是 | 回调表示成功还是失败。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
文件管理错误码
](
../errorcodes/errorcode-filemanagement.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13900042 | Unknown error. |
**示例:**
```
ts
async
function
example
()
{
console
.
info
(
'
releaseDemo
'
);
phAccessHelper
.
release
((
err
)
=>
{
if
(
err
!=
undefined
)
{
console
.
error
(
'
release failed. message =
'
,
err
);
}
else
{
console
.
info
(
'
release ok.
'
);
}
});
}
```
### release
release(): Promise
<
void
>
释放PhotoAccessHelper实例。
当后续不需要使用PhotoAccessHelper 实例中的方法时调用。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**返回值:**
| 类型 | 说明 |
| ------------------- | --------------------------------- |
| Promise
<
void
>
| Promise对象,返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
文件管理错误码
](
../errorcodes/errorcode-filemanagement.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13900042 | Unknown error. |
**示例:**
```
ts
async
function
example
()
{
console
.
info
(
'
releaseDemo
'
);
try
{
await
phAccessHelper
.
release
();
console
.
info
(
'
release ok.
'
);
}
catch
(
err
)
{
console
.
error
(
'
release failed. message =
'
,
err
);
}
}
```
## PhotoAsset
提供封装文件属性的方法。
### 属性
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
| 名称 | 类型 | 可读 | 可写 | 说明 |
| ------------------------- | ------------------------ | ---- | ---- | ------------------------------------------------------ |
| uri | string | 是 | 否 | 文件资源uri(如:file://media/image/2)。 |
| photoType |
[
PhotoType
](
#phototype
)
| 是 | 否 | 媒体文件类型 |
| displayName | string | 是 | 否 | 显示文件名,包含后缀名。 |
### get
get(member: string): MemberType;
获取PhotoAsset成员参数。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ----- |
| member | string | 是 | 成员参数名称例如:PhotoKeys.URI。 |
**返回值:**
| 类型 | 说明 |
| ------------------- | --------------------------------- |
|
[
MemberType
](
#membertype
)
| 获取PhotoAsset成员参数的值。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
photoAssetGetDemo
'
);
try
{
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[
'
title
'
],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
let
photoAsset
=
await
fetchResult
.
getFirstObject
();
let
title
=
photoAccessHelper
.
PhotoKeys
.
TITLE
;
let
photoAssetTitle
=
photoAsset
.
get
(
title
.
toString
());
console
.
info
(
'
photoAsset Get photoAssetTitle =
'
,
photoAssetTitle
);
}
catch
(
err
)
{
console
.
error
(
'
release failed. message =
'
,
err
);
}
}
```
### set
set(member: string, value: string): void;
设置PhotoAsset成员参数。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ----- |
| member | string | 是 | 成员参数名称例如:PhotoKeys.URI。 |
| value | string | 是 | 设置成员参数名称,只能修改PhotoKeys.TITLE的值。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
photoAssetSetDemo
'
);
try
{
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[
'
title
'
],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
let
photoAsset
=
await
fetchResult
.
getFirstObject
();
let
title
=
photoAccessHelper
.
PhotoKeys
.
TITLE
.
toString
();
photoAsset
.
set
(
title
,
'
newTitle
'
);
}
catch
(
err
)
{
console
.
error
(
'
release failed. message =
'
,
err
);
}
}
```
### commitModify
commitModify(callback: AsyncCallback
<
void
>
): void
修改文件的元数据,使用callback方式返回异步结果。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ----- |
| callback | AsyncCallback
<
void
>
| 是 | callback返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if values to commit is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
commitModifyDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[
'
title
'
],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
let
photoAsset
=
await
fetchResult
.
getFirstObject
();
let
title
=
photoAccessHelper
.
PhotoKeys
.
TITLE
.
toString
();
let
photoAssetTitle
=
photoAsset
.
get
(
title
);
console
.
info
(
'
photoAsset get photoAssetTitle =
'
,
photoAssetTitle
);
photoAsset
.
set
(
title
,
'
newTitle2
'
);
photoAsset
.
commitModify
((
err
)
=>
{
if
(
err
==
undefined
)
{
let
newPhotoAssetTitle
=
photoAsset
.
get
(
title
);
console
.
info
(
'
photoAsset get newPhotoAssetTitle =
'
,
newPhotoAssetTitle
);
}
else
{
console
.
error
(
'
commitModify failed, message =
'
,
err
);
}
});
}
```
### commitModify
commitModify(): Promise
<
void
>
修改文件的元数据,使用promise方式返回异步结果。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**返回值:**
| 类型 | 说明 |
| ------------------- | ---------- |
| Promise
<
void
>
| Promise对象,返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if values to commit is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
commitModifyDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[
'
title
'
],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
let
photoAsset
=
await
fetchResult
.
getFirstObject
();
let
title
=
photoAccessHelper
.
PhotoKeys
.
TITLE
.
toString
();
let
photoAssetTitle
=
photoAsset
.
get
(
title
);
console
.
info
(
'
photoAsset get photoAssetTitle =
'
,
photoAssetTitle
);
photoAsset
.
set
(
title
,
'
newTitle3
'
);
try
{
await
photoAsset
.
commitModify
();
let
newPhotoAssetTitle
=
photoAsset
.
get
(
title
);
console
.
info
(
'
photoAsset get newPhotoAssetTitle =
'
,
newPhotoAssetTitle
);
}
catch
(
err
)
{
console
.
error
(
'
release failed. message =
'
,
err
);
}
}
```
### open
open(mode: string, callback: AsyncCallback
<
number
>
): void
打开当前文件,使用callback方式返回异步结果。
**注意**
:当前写操作是互斥的操作,写操作完成后需要调用close进行释放。
**系统接口**
:此接口为系统接口。
**需要权限**
:ohos.permission.READ_IMAGEVIDEO 或 ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ----------------------------------- |
| mode | string | 是 | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写)。 |
| callback | AsyncCallback
<
number
>
| 是 | callback返回文件描述符。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if parameter is invalid. |
**示例:**
```
ts
async
function
example
()
{
console
.
info
(
'
openDemo
'
);
let
testFileName
=
'
testFile
'
+
Date
.
now
()
+
'
.jpg
'
;
const
photoAsset
=
await
phAccessHelper
.
createAsset
(
testFileName
);
photoAsset
.
open
(
'
rw
'
,
(
err
,
fd
)
=>
{
if
(
fd
!=
undefined
)
{
console
.
info
(
'
File fd
'
+
fd
);
photoAsset
.
close
(
fd
);
}
else
{
console
.
error
(
'
File err
'
+
err
);
}
});
}
```
### open
open(mode: string): Promise
<
number
>
打开当前文件,使用promise方式返回异步结果。
**注意**
:当前写操作是互斥的操作,写操作完成后需要调用close进行释放。
**系统接口**
:此接口为系统接口。
**需要权限**
:ohos.permission.READ_IMAGEVIDEO 或 ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------------------------------- |
| mode | string | 是 | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写)。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------- |
| Promise
<
number
>
| Promise对象,返回文件描述符。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if parameter is invalid. |
**示例:**
```
ts
async
function
example
()
{
console
.
info
(
'
openDemo
'
);
try
{
let
testFileName
=
'
testFile
'
+
Date
.
now
()
+
'
.jpg
'
;
const
photoAsset
=
await
phAccessHelper
.
createAsset
(
testFileName
);
let
fd
=
await
photoAsset
.
open
(
'
rw
'
);
if
(
fd
!=
undefined
)
{
console
.
info
(
'
File fd
'
+
fd
);
photoAsset
.
close
(
fd
);
}
else
{
console
.
error
(
'
open File fail
'
);
}
}
catch
(
err
)
{
console
.
error
(
'
open Demo err
'
+
err
);
}
}
```
### getReadOnlyFd
getReadOnlyFd(callback: AsyncCallback
<
number
>
): void
以只读方式打开当前文件,使用callback方式返回异步结果。
**注意**
:读操作完成后需要调用close进行释放。
**需要权限**
:ohos.permission.READ_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ----------------------------------- |
| callback | AsyncCallback
<
number
>
| 是 | callback返回文件描述符。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if parameter is invalid. |
**示例:**
```
ts
async
function
example
()
{
console
.
info
(
'
getReadOnlyFdDemo
'
);
let
testFileName
=
'
testFile
'
+
Date
.
now
()
+
'
.jpg
'
;
const
photoAsset
=
await
phAccessHelper
.
createAsset
(
testFileName
);
photoAsset
.
getReadOnlyFd
((
err
,
fd
)
=>
{
if
(
fd
!=
undefined
)
{
console
.
info
(
'
File fd
'
+
fd
);
photoAsset
.
close
(
fd
);
}
else
{
console
.
error
(
'
File err
'
+
err
);
}
});
}
```
### getReadOnlyFd
getReadOnlyFd(): Promise
<
number
>
以只读方式打开当前文件,使用promise方式返回异步结果。
**注意**
:读操作完成后需要调用close进行释放。
**需要权限**
:ohos.permission.READ_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------- |
| Promise
<
number
>
| Promise对象,返回文件描述符。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if parameter is invalid. |
**示例:**
```
ts
async
function
example
()
{
console
.
info
(
'
getReadOnlyFdDemo
'
);
try
{
let
testFileName
=
'
testFile
'
+
Date
.
now
()
+
'
.jpg
'
;
const
photoAsset
=
await
phAccessHelper
.
createAsset
(
testFileName
);
let
fd
=
await
photoAsset
.
getReadOnlyFd
();
if
(
fd
!=
undefined
)
{
console
.
info
(
'
File fd
'
+
fd
);
photoAsset
.
close
(
fd
);
}
else
{
console
.
error
(
'
open File fail
'
);
}
}
catch
(
err
)
{
console
.
error
(
'
open Demo err
'
+
err
);
}
}
```
### close
close(fd: number, callback: AsyncCallback
<
void
>
): void
关闭当前文件,使用callback方式返回异步结果。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ----- |
| fd | number | 是 | 文件描述符。 |
| callback | AsyncCallback
<
void
>
| 是 | callback返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
closeDemo
'
);
try
{
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
const
photoAsset
=
await
fetchResult
.
getFirstObject
();
let
fd
=
await
photoAsset
.
open
(
'
rw
'
);
console
.
info
(
'
file fd
'
,
fd
);
photoAsset
.
close
(
fd
,
(
err
)
=>
{
if
(
err
==
undefined
)
{
console
.
info
(
'
asset close succeed.
'
);
}
else
{
console
.
error
(
'
close failed, message =
'
+
err
);
}
});
}
catch
(
err
)
{
console
.
error
(
'
close failed, message =
'
+
err
);
}
}
```
### close
close(fd: number): Promise
<
void
>
关闭当前文件,使用promise方式返回异步结果。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----- |
| fd | number | 是 | 文件描述符。 |
**返回值:**
| 类型 | 说明 |
| ------------------- | ---------- |
| Promise
<
void
>
| Promise对象,返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
closeDemo
'
);
try
{
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
const
asset
=
await
fetchResult
.
getFirstObject
();
let
fd
=
await
asset
.
open
(
'
rw
'
);
console
.
info
(
'
file fd
'
,
fd
);
await
asset
.
close
(
fd
);
console
.
info
(
'
asset close succeed.
'
);
}
catch
(
err
)
{
console
.
error
(
'
close failed, message =
'
+
err
);
}
}
```
### getThumbnail
getThumbnail(callback: AsyncCallback
<
image.PixelMap
>
): void
获取文件的缩略图,使用callback方式返回异步结果。
**需要权限**
:ohos.permission.READ_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------- | ---- | ---------------- |
| callback | AsyncCallback
<
[image.PixelMap](js-apis-image.md#pixelmap7)
>
| 是 | callback返回缩略图的PixelMap。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
getThumbnailDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
const
asset
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
asset displayName =
'
,
asset
.
displayName
);
asset
.
getThumbnail
((
err
,
pixelMap
)
=>
{
if
(
err
==
undefined
)
{
console
.
info
(
'
getThumbnail successful
'
+
pixelMap
);
}
else
{
console
.
error
(
'
getThumbnail fail
'
,
err
);
}
});
}
```
### getThumbnail
getThumbnail(size: image.Size, callback: AsyncCallback
<
image.PixelMap
>
): void
获取文件的缩略图,传入缩略图尺寸,使用callback方式返回异步结果。
**需要权限**
:ohos.permission.READ_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------------------- | ---- | ---------------- |
| size |
[
image.Size
](
js-apis-image.md#size
)
| 是 | 缩略图尺寸。 |
| callback | AsyncCallback
<
[image.PixelMap](js-apis-image.md#pixelmap7)
>
| 是 | callback返回缩略图的PixelMap。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
getThumbnailDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
size
=
{
width
:
720
,
height
:
720
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
const
asset
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
asset displayName =
'
,
asset
.
displayName
);
asset
.
getThumbnail
(
size
,
(
err
,
pixelMap
)
=>
{
if
(
err
==
undefined
)
{
console
.
info
(
'
getThumbnail successful
'
+
pixelMap
);
}
else
{
console
.
error
(
'
getThumbnail fail
'
,
err
);
}
});
}
```
### getThumbnail
getThumbnail(size?: image.Size): Promise
<
image.PixelMap
>
获取文件的缩略图,传入缩略图尺寸,使用promise方式返回异步结果。
**需要权限**
:ohos.permission.READ_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---- | -------------- | ---- | ----- |
| size |
[
image.Size
](
js-apis-image.md#size
)
| 否 | 缩略图尺寸。 |
**返回值:**
| 类型 | 说明 |
| ----------------------------- | --------------------- |
| Promise
<
[image.PixelMap](js-apis-image.md#pixelmap7)
>
| Promise对象,返回缩略图的PixelMap。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
getThumbnailDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
size
=
{
width
:
720
,
height
:
720
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
const
asset
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
asset displayName =
'
,
asset
.
displayName
);
asset
.
getThumbnail
(
size
).
then
((
pixelMap
)
=>
{
console
.
info
(
'
getThumbnail successful
'
+
pixelMap
);
}).
catch
((
err
)
=>
{
console
.
error
(
'
getThumbnail fail
'
+
err
);
});
}
```
### setFavorite
setFavorite(favoriteState: boolean, callback: AsyncCallback
<
void
>
): void
将文件设置为收藏文件,使用callback方式返回异步结果。
**系统接口**
:此接口为系统接口。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---------- | ------------------------- | ---- | ---------------------------------- |
| favoriteState | boolean | 是 | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏。 |
| callback | AsyncCallback
<
void
>
| 是 | callback返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
setFavoriteDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
const
asset
=
await
fetchResult
.
getFirstObject
();
asset
.
setFavorite
(
true
,
(
err
)
=>
{
if
(
err
==
undefined
)
{
console
.
info
(
'
favorite successfully
'
);
}
else
{
console
.
error
(
'
favorite failed with error:
'
+
err
);
}
});
}
```
### setFavorite
setFavorite(favoriteState: boolean): Promise
<
void
>
将文件设置为收藏文件,使用promise方式返回异步结果。
**系统接口**
:此接口为系统接口。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---------- | ------- | ---- | ---------------------------------- |
| favoriteState | boolean | 是 | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏。 |
**返回值:**
| 类型 | 说明 |
| ------------------- | ---------- |
| Promise
<
void
>
| Promise对象,返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
setFavoriteDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
const
asset
=
await
fetchResult
.
getFirstObject
();
asset
.
setFavorite
(
true
).
then
(
function
()
{
console
.
info
(
'
setFavorite successfully
'
);
}).
catch
(
function
(
err
)
{
console
.
error
(
'
setFavorite failed with error:
'
+
err
);
});
}
```
### setHidden
setHidden(hiddenState: boolean, callback: AsyncCallback
<
void
>
): void
将文件设置为隐私文件,使用callback方式返回异步结果。
隐私文件存在隐私相册中,用户通过隐私相册去获取隐私文件后可以通过设置hiddenState为false来从隐私相册中移除。
**系统接口**
:此接口为系统接口。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---------- | ------------------------- | ---- | ---------------------------------- |
| hiddenState | boolean | 是 | 是否设置为隐藏文件,true:将文件资产放入隐藏相册;false:从隐藏相册中恢复。 |
| callback | AsyncCallback
<
void
>
| 是 | callback返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
setHiddenDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
const
asset
=
await
fetchResult
.
getFirstObject
();
asset
.
setHidden
(
true
,
(
err
)
=>
{
if
(
err
==
undefined
)
{
console
.
info
(
'
setHidden successfully
'
);
}
else
{
console
.
error
(
'
setHidden failed with error:
'
+
err
);
}
});
}
```
### setHidden
setHidden(hiddenState: boolean): Promise
<
void
>
将文件设置为隐私文件,使用promise方式返回异步结果。
隐私文件存在隐私相册中,用户通过隐私相册去获取隐私文件后可以通过设置hiddenState为false来从隐私相册中移除。
**系统接口**
:此接口为系统接口。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---------- | ------- | ---- | ---------------------------------- |
| hiddenState | boolean | 是 | 是否设置为隐藏文件,true:将文件资产放入隐藏相册;false:从隐藏相册中恢复。 |
**返回值:**
| 类型 | 说明 |
| ------------------- | ---------- |
| Promise
<
void
>
| Promise对象,返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if parameter is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
// 示例代码为将文件从隐藏相册中恢复,需要先在隐藏相册预置资源
console
.
info
(
'
setHiddenDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
albumList
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
SYSTEM
,
photoAccessHelper
.
AlbumSubtype
.
HIDDEN
);
const
album
=
await
albumList
.
getFirstObject
();
let
fetchResult
=
await
album
.
getAssets
(
fetchOption
);
const
asset
=
await
fetchResult
.
getFirstObject
();
asset
.
setHidden
(
false
).
then
(()
=>
{
console
.
info
(
'
setHidden successfully
'
);
}).
catch
((
err
)
=>
{
console
.
error
(
'
setHidden failed with error:
'
+
err
);
});
}
```
## FetchResult
文件检索结果集。
### getCount
getCount(): number
获取文件检索结果中的文件总数。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**返回值:**
| 类型 | 说明 |
| ------ | -------- |
| number | 检索到的文件总数。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
文件管理错误码
](
../errorcodes/errorcode-filemanagement.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13900042 | Unknown error. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
getCountDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
const
fetchCount
=
fetchResult
.
getCount
();
console
.
info
(
'
fetchCount =
'
,
fetchCount
);
}
```
### isAfterLast
isAfterLast(): boolean
检查结果集是否指向最后一行。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**返回值:**
| 类型 | 说明 |
| ------- | ---------------------------------- |
| boolean | 当读到最后一条记录后,后续没有记录返回true,否则返回false。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
文件管理错误码
](
../errorcodes/errorcode-filemanagement.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13900042 | Unknown error. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
const
fetchCount
=
fetchResult
.
getCount
();
console
.
info
(
'
count:
'
+
fetchCount
);
let
photoAsset
=
await
fetchResult
.
getLastObject
();
if
(
fetchResult
.
isAfterLast
())
{
console
.
info
(
'
photoAsset isAfterLast displayName =
'
,
photoAsset
.
displayName
);
}
else
{
console
.
info
(
'
photoAsset not isAfterLast
'
);
}
}
```
### close
close(): void
释放FetchFileResult实例并使其失效。无法调用其他方法。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**错误码:**
接口抛出错误码的详细介绍请参见
[
文件管理错误码
](
../errorcodes/errorcode-filemanagement.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13900042 | Unknown error. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
fetchResultCloseDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
try
{
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
fetchResult
.
close
();
console
.
info
(
'
close succeed.
'
);
}
catch
(
err
)
{
console
.
error
(
'
close fail. message =
'
+
err
);
}
}
```
### getFirstObject
getFirstObject(callback: AsyncCallback
<
T
>
): void
获取文件检索结果中的第一个文件资产。此方法使用callback形式返回结果。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------------- | ---- | ------------------------------------------- |
| callback | AsyncCallback
<
T
>
| 是 | 异步获取结果集中的第一个完成后的回调。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
文件管理错误码
](
../errorcodes/errorcode-filemanagement.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13900042 | Unknown error. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
getFirstObjectDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
fetchResult
.
getFirstObject
((
err
,
photoAsset
)
=>
{
if
(
photoAsset
!=
undefined
)
{
console
.
info
(
'
photoAsset displayName:
'
,
photoAsset
.
displayName
);
}
else
{
console
.
error
(
'
photoAsset failed with err:
'
+
err
);
}
});
}
```
### getFirstObject
getFirstObject(): Promise
<
T
>
获取文件检索结果中的第一个文件资产。此方法使用promise方式来异步返回。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**返回值:**
| 类型 | 说明 |
| --------------------------------------- | -------------------------- |
| Promise
<
T
>
| Promise对象,返回结果集中第一个对象。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
文件管理错误码
](
../errorcodes/errorcode-filemanagement.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13900042 | Unknown error. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
getFirstObjectDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
let
photoAsset
=
await
fetchResult
.
getFirstObject
();
console
.
info
(
'
photoAsset displayName:
'
,
photoAsset
.
displayName
);
}
```
### getNextObject
getNextObject(callback: AsyncCallback
<
T
>
): void
获取文件检索结果中的下一个文件资产。此方法使用callback形式返回结果。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | --------------------------------------------- | ---- | ----------------------------------------- |
| callbacke | AsyncCallback
<
T
>
| 是 | 异步返回结果集中下一个之后的回调。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
文件管理错误码
](
../errorcodes/errorcode-filemanagement.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13900042 | Unknown error. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
getNextObjectDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
await
fetchResult
.
getFirstObject
();
if
(
fetchResult
.
isAfterLast
())
{
fetchResult
.
getNextObject
((
err
,
photoAsset
)
=>
{
if
(
photoAsset
!=
undefined
)
{
console
.
info
(
'
photoAsset displayName:
'
,
photoAsset
.
displayName
);
}
else
{
console
.
error
(
'
photoAsset failed with err:
'
+
err
);
}
});
}
}
```
### getNextObject
getNextObject(): Promise
<
T
>
获取文件检索结果中的下一个文件资产。此方法使用promise方式来异步返回。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**返回值:**
| 类型 | 说明 |
| --------------------------------------- | ----------------- |
| Promise
<
T
>
| Promise对象,返回结果集中下一个对象。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
文件管理错误码
](
../errorcodes/errorcode-filemanagement.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13900042 | Unknown error. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
getNextObjectDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
await
fetchResult
.
getFirstObject
();
if
(
fetchResult
.
isAfterLast
())
{
let
photoAsset
=
await
fetchResult
.
getNextObject
();
console
.
info
(
'
photoAsset displayName:
'
,
photoAsset
.
displayName
);
}
}
```
### getLastObject
getLastObject(callback: AsyncCallback
<
T
>
): void
获取文件检索结果中的最后一个文件资产。此方法使用callback回调来返回。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------------- | ---- | --------------------------- |
| callback | AsyncCallback
<
T
>
| 是 | 异步返回结果集中最后一个的回调。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
文件管理错误码
](
../errorcodes/errorcode-filemanagement.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13900042 | Unknown error. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
getLastObjectDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
fetchResult
.
getLastObject
((
err
,
photoAsset
)
=>
{
if
(
photoAsset
!=
undefined
)
{
console
.
info
(
'
photoAsset displayName:
'
,
photoAsset
.
displayName
);
}
else
{
console
.
error
(
'
photoAsset failed with err:
'
+
err
);
}
});
}
```
### getLastObject
getLastObject(): Promise
<
T
>
获取文件检索结果中的最后一个文件资产。此方法使用Promise方式来返回。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**返回值:**
| 类型 | 说明 |
| --------------------------------------- | ----------------- |
| Promise
<
T
>
| Promise对象,返回结果集中最后一个对象。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
文件管理错误码
](
../errorcodes/errorcode-filemanagement.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13900042 | Unknown error. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
getLastObjectDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
let
photoAsset
=
await
fetchResult
.
getLastObject
();
console
.
info
(
'
photoAsset displayName:
'
,
photoAsset
.
displayName
);
}
```
### getObjectByPosition
getObjectByPosition(index: number, callback: AsyncCallback
<
T
>
): void
获取文件检索结果中具有指定索引的文件资产。此方法使用callback来返回。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ------------------ |
| index | number | 是 | 要获取的文件的索引,从0开始。 |
| callback | AsyncCallback
<
T
>
| 是 | 异步返回指定索引的文件资产的回调。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if type index is not number. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
getObjectByPositionDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
fetchResult
.
getObjectByPosition
(
0
,
(
err
,
photoAsset
)
=>
{
if
(
photoAsset
!=
undefined
)
{
console
.
info
(
'
photoAsset displayName:
'
,
photoAsset
.
displayName
);
}
else
{
console
.
error
(
'
photoAsset failed with err:
'
+
err
);
}
});
}
```
### getObjectByPosition
getObjectByPosition(index: number): Promise
<
T
>
获取文件检索结果中具有指定索引的文件资产。此方法使用Promise形式返回文件Asset。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | -------------- |
| index | number | 是 | 要获取的文件的索引,从0开始。 |
**返回值:**
| 类型 | 说明 |
| --------------------------------------- | ----------------- |
| Promise
<
T
>
| Promise对象,返回结果集中指定索引的一个对象。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if type index is not number. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
getObjectByPositionDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
let
photoAsset
=
await
fetchResult
.
getObjectByPosition
(
0
);
console
.
info
(
'
photoAsset displayName:
'
,
photoAsset
.
displayName
);
}
```
### getAllObjects
getAllObjects(callback: AsyncCallback
<
Array
<
T
>>
): void
获取文件检索结果中的所有文件资产。此方法使用callback形式返回结果。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------------------------- | ---- | ------------------------------------------- |
| callback | AsyncCallback
<
Array
<
T
>>
| 是 | 异步获取结果集中的所有文件资产完成后的回调。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
文件管理错误码
](
../errorcodes/errorcode-filemanagement.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13900042 | Unknown error. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
getAllObjectDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
fetchResult
.
getAllObjects
((
err
,
photoAssetList
)
=>
{
if
(
photoAssetList
!=
undefined
)
{
console
.
info
(
'
photoAssetList length:
'
,
photoAssetList
.
length
);
}
else
{
console
.
error
(
'
photoAssetList failed with err:
'
+
err
);
}
});
}
```
### getAllObjects
getAllObjects(): Promise
<
Array
<
T
>>
获取文件检索结果中的所有文件资产。此方法使用promise方式来异步返回。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**返回值:**
| 类型 | 说明 |
| --------------------------------------- | -------------------------- |
| Promise
<
Array
<
T
>>
| Promise对象,返回结果集中所有文件资产数组。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
文件管理错误码
](
../errorcodes/errorcode-filemanagement.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 13900042 | Unknown error. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
getAllObjectDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
let
photoAssetList
=
await
fetchResult
.
getAllObjects
();
console
.
info
(
'
photoAssetList length:
'
,
photoAssetList
.
length
);
}
```
## Album
实体相册
### 属性
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
| 名称 | 类型 | 可读 | 可写 | 说明 |
| ------------ | ------ | ---- | ---- | ------- |
| albumType |
[
AlbumType
](
#albumtype
)
| 是 | 否 | 相册类型。 |
| albumSubtype |
[
AlbumSubtype
](
#albumsubtype
)
| 是 | 否 | 相册子类型。 |
| albumName | string | 是 | 用户相册可写,预置相册不可写 | 相册名称。 |
| albumUri | string | 是 | 否 | 相册Uri。 |
| count | number | 是 | 否 | 相册中文件数量。 |
| coverUri | string | 是 | 否 | 封面文件Uri。 |
### getAssets
getAssets(options: FetchOptions, callback: AsyncCallback
<
FetchResult
<
PhotoAsset
>>
): void;
获取相册中的文件。该方法使用callback形式来返回文件。
**需要权限**
:ohos.permission.READ_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------- |
| options |
[
FetchOptions
](
#fetchoptions
)
| 是 | 检索选项。 |
| callback | AsyncCallback
<
[FetchResult](#fetchresult)
<
[PhotoAsset](#photoasset)
>>
| 是 | callback返回图片和视频数据结果集。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if type options is not FetchOptions. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
albumGetPhotoAssetsDemoCallback
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
albumFetchOptions
=
{
predicates
:
predicates
};
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
const
albumList
=
await
phAccessHelper
.
getAlbums
(
albumFetchOptions
);
const
album
=
await
albumList
.
getFirstObject
();
album
.
getAssets
(
fetchOption
,
(
err
,
albumFetchResult
)
=>
{
if
(
albumFetchResult
!=
undefined
)
{
console
.
info
(
'
album getAssets successfully, getCount:
'
+
albumFetchResult
.
getCount
());
}
else
{
console
.
error
(
'
album getAssets failed with error:
'
+
err
);
}
});
}
```
### getAssets
getAssets(options: FetchOptions): Promise
<
FetchResult
<
PhotoAsset
>>
;
获取相册中的文件。该方法使用Promise来返回文件。
**需要权限**
:ohos.permission.READ_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------- |
| options |
[
FetchOptions
](
#fetchoptions
)
| 是 | 检索选项。 |
**返回值:**
| 类型 | 说明 |
| --------------------------------------- | ----------------- |
| Promise
<
[FetchResult](#fetchresult)
<
[PhotoAsset](#photoasset)
>>
| Promise对象,返回图片和视频数据结果集。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if type options is not FetchOptions. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
albumGetPhotoAssetsDemoPromise
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
albumFetchOptions
=
{
predicates
:
predicates
};
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
const
albumList
=
await
phAccessHelper
.
getAlbums
(
albumFetchOptions
);
const
album
=
await
albumList
.
getFirstObject
();
album
.
getAssets
(
fetchOption
).
then
((
albumFetchResult
)
=>
{
console
.
info
(
'
album getPhotoAssets successfully, getCount:
'
+
albumFetchResult
.
getCount
());
}).
catch
((
err
)
=>
{
console
.
error
(
'
album getPhotoAssets failed with error:
'
+
err
);
});
}
```
### commitModify
commitModify(callback: AsyncCallback
<
void
>
): void;
更新相册属性修改到数据库中。该方法使用callback形式来返回结果。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback
<
void
>
| 是 | callback返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if value to modify is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
albumCommitModifyDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
albumFetchOptions
=
{
predicates
:
predicates
};
const
albumList
=
await
phAccessHelper
.
getAlbums
(
albumFetchOptions
);
const
album
=
await
albumList
.
getFirstObject
();
album
.
albumName
=
'
hello
'
;
album
.
commitModify
((
err
)
=>
{
if
(
err
!=
undefined
)
{
console
.
error
(
'
commitModify failed with error:
'
+
err
);
}
else
{
console
.
info
(
'
commitModify successfully
'
);
}
});
}
```
### commitModify
commitModify(): Promise
<
void
>
;
更新相册属性修改到数据库中。该方法使用Promise来返回结果。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**返回值:**
| 类型 | 说明 |
| ------------------- | ------------ |
| Promise
<
void
>
| Promise对象,返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if value to modify is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
console
.
info
(
'
albumCommitModifyDemo
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
albumFetchOptions
=
{
predicates
:
predicates
};
const
albumList
=
await
phAccessHelper
.
getAlbums
(
albumFetchOptions
);
const
album
=
await
albumList
.
getFirstObject
();
album
.
albumName
=
'
hello
'
;
album
.
commitModify
().
then
(()
=>
{
console
.
info
(
'
commitModify successfully
'
);
}).
catch
((
err
)
=>
{
console
.
error
(
'
commitModify failed with error:
'
+
err
);
});
}
```
### addAssets
addAssets(assets: Array
<
PhotoAsset
>
, callback: AsyncCallback
<
void
>
): void;
往相册中添加图片或者视频,需要先预置相册和文件资源。该方法使用callback形式来返回结果。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------- |
| assets | Array
<
[PhotoAsset](#photoasset)
>
| 是 | 待添加到相册中的图片或视频数组。 |
| callback | AsyncCallback
<
void
>
| 是 | callback返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if PhotoAssets is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
try
{
console
.
info
(
'
addAssetsDemoCallback
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
albumFetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
USER
,
photoAccessHelper
.
AlbumSubtype
.
USER_GENERIC
);
let
album
=
await
albumFetchResult
.
getFirstObject
();
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
let
asset
=
await
fetchResult
.
getFirstObject
();
album
.
addAssets
([
asset
],
(
err
)
=>
{
if
(
err
===
undefined
)
{
console
.
info
(
'
album addAssets successfully
'
);
}
else
{
console
.
error
(
'
album addAssets failed with error:
'
+
err
);
}
});
}
catch
(
err
)
{
console
.
error
(
'
addAssetsDemoCallback failed with error:
'
+
err
);
}
}
```
### addAssets
addAssets(assets: Array
<
PhotoAsset
>
): Promise
<
void
>
;
往相册中添加图片或者视频,需要先预置相册和文件资源。该方法使用Promise来返回结果。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------- |
| assets | Array
<
[PhotoAsset](#photoasset)
>
| 是 | 待添加到相册中的图片或视频数组。 |
**返回值:**
| 类型 | 说明 |
| --------------------------------------- | ----------------- |
|Promise
<
void
>
| Promise对象,返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if PhotoAssets is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
try
{
console
.
info
(
'
addAssetsDemoPromise
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
albumFetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
USER
,
photoAccessHelper
.
AlbumSubtype
.
USER_GENERIC
);
let
album
=
await
albumFetchResult
.
getFirstObject
();
let
fetchResult
=
await
phAccessHelper
.
getAssets
(
fetchOption
);
let
asset
=
await
fetchResult
.
getFirstObject
();
album
.
addAssets
([
asset
]).
then
(()
=>
{
console
.
info
(
'
album addAssets successfully
'
);
}).
catch
((
err
)
=>
{
console
.
error
(
'
album addAssets failed with error:
'
+
err
);
});
}
catch
(
err
)
{
console
.
error
(
'
addAssetsDemoPromise failed with error:
'
+
err
);
}
}
```
### removeAssets
removeAssets(assets: Array
<
PhotoAsset
>
, callback: AsyncCallback
<
void
>
): void;
从相册中移除图片或者视频,需要先预置相册和文件资源。该方法使用callback形式来返回结果。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------- |
| assets | Array
<
[PhotoAsset](#photoasset)
>
| 是 | 相册中待移除的图片或视频数组。 |
| callback | AsyncCallback
<
void
>
| 是 | callback返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if PhotoAssets is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
try
{
console
.
info
(
'
removeAssetsDemoCallback
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
albumFetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
USER
,
photoAccessHelper
.
AlbumSubtype
.
USER_GENERIC
);
let
album
=
await
albumFetchResult
.
getFirstObject
();
let
fetchResult
=
await
album
.
getAssets
(
fetchOption
);
let
asset
=
await
fetchResult
.
getFirstObject
();
album
.
removeAssets
([
asset
],
(
err
)
=>
{
if
(
err
===
undefined
)
{
console
.
info
(
'
album removeAssets successfully
'
);
}
else
{
console
.
error
(
'
album removeAssets failed with error:
'
+
err
);
}
});
}
catch
(
err
)
{
console
.
error
(
'
removeAssetsDemoCallback failed with error:
'
+
err
);
}
}
```
### removeAssets
removeAssets(assets: Array
<
PhotoAsset
>
): Promise
<
void
>
;
从相册中移除图片或者视频,需要先预置相册和文件资源。该方法使用Promise来返回结果。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------- |
| assets | Array
<
[PhotoAsset](#photoasset)
>
| 是 | 相册中待移除的图片或视频数组。 |
**返回值:**
| 类型 | 说明 |
| --------------------------------------- | ----------------- |
|Promise
<
void
>
| Promise对象,返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | if PhotoAssets is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
try
{
console
.
info
(
'
removeAssetsDemoPromise
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
albumFetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
USER
,
photoAccessHelper
.
AlbumSubtype
.
USER_GENERIC
);
let
album
=
await
albumFetchResult
.
getFirstObject
();
let
fetchResult
=
await
album
.
getAssets
(
fetchOption
);
let
asset
=
await
fetchResult
.
getFirstObject
();
album
.
removeAssets
([
asset
]).
then
(()
=>
{
console
.
info
(
'
album removeAssets successfully
'
);
}).
catch
((
err
)
=>
{
console
.
error
(
'
album removeAssets failed with error:
'
+
err
);
});
}
catch
(
err
)
{
console
.
error
(
'
removeAssetsDemoPromise failed with error:
'
+
err
);
}
}
```
### recoverAssets
recoverAssets(assets: Array
<
PhotoAsset
>
, callback: AsyncCallback
<
void
>
): void;
从回收站中恢复图片或者视频,需要先在回收站中预置文件资源。该方法使用callback形式来返回结果。
**系统接口**
:此接口为系统接口。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------- |
| assets | Array
<
[PhotoAsset](#photoasset)
>
| 是 | 回收站中待恢复图片或者视频数组。 |
| callback | AsyncCallback
<
void
>
| 是 | callback返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if PhotoAssets is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
try
{
console
.
info
(
'
recoverAssetsDemoCallback
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
albumFetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
SYSTEM
,
photoAccessHelper
.
AlbumSubtype
.
TRASH
);
let
album
=
await
albumFetchResult
.
getFirstObject
();
let
fetchResult
=
await
album
.
getAssets
(
fetchOption
);
let
asset
=
await
fetchResult
.
getFirstObject
();
album
.
recoverAssets
([
asset
],
(
err
)
=>
{
if
(
err
===
undefined
)
{
console
.
info
(
'
album recoverAssets successfully
'
);
}
else
{
console
.
error
(
'
album recoverAssets failed with error:
'
+
err
);
}
});
}
catch
(
err
)
{
console
.
error
(
'
recoverAssetsDemoCallback failed with error:
'
+
err
);
}
}
```
### recoverAssets
recoverAssets(assets: Array
<
PhotoAsset
>
): Promise
<
void
>
;
从回收站中恢复图片或者视频,需要先在回收站中预置文件资源。该方法使用Promise来返回结果。
**系统接口**
:此接口为系统接口。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------- |
| assets | Array
<
[PhotoAsset](#photoasset)
>
| 是 | 回收站中待恢复图片或者视频数组。 |
**返回值:**
| 类型 | 说明 |
| --------------------------------------- | ----------------- |
|Promise
<
void
>
| Promise对象,返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if PhotoAssets is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
try
{
console
.
info
(
'
recoverAssetsDemoPromise
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
albumFetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
SYSTEM
,
photoAccessHelper
.
AlbumSubtype
.
TRASH
);
let
album
=
await
albumFetchResult
.
getFirstObject
();
let
fetchResult
=
await
album
.
getAssets
(
fetchOption
);
let
asset
=
await
fetchResult
.
getFirstObject
();
album
.
recoverAssets
([
asset
]).
then
(()
=>
{
console
.
info
(
'
album recoverAssets successfully
'
);
}).
catch
((
err
)
=>
{
console
.
error
(
'
album recoverAssets failed with error:
'
+
err
);
});
}
catch
(
err
)
{
console
.
error
(
'
recoverAssetsDemoPromise failed with error:
'
+
err
);
}
}
```
### deleteAssets
deleteAssets(assets: Array
<
PhotoAsset
>
, callback: AsyncCallback
<
void
>
): void;
从回收站中彻底删除图片或者视频,需要先在回收站中预置文件资源。该方法使用callback形式来返回结果。
**注意**
:此操作不可逆,执行此操作后文件资源将彻底删除,请谨慎操作。
**系统接口**
:此接口为系统接口。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------- |
| assets | Array
<
[PhotoAsset](#photoasset)
>
| 是 | 回收站中待彻底删除图片或者视频数组。 |
| callback | AsyncCallback
<
void
>
| 是 | callback返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if PhotoAssets is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
try
{
console
.
info
(
'
deleteAssetsDemoCallback
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
albumFetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
SYSTEM
,
photoAccessHelper
.
AlbumSubtype
.
TRASH
);
let
album
=
await
albumFetchResult
.
getFirstObject
();
let
fetchResult
=
await
album
.
getAssets
(
fetchOption
);
let
asset
=
await
fetchResult
.
getFirstObject
();
album
.
deleteAssets
([
asset
],
(
err
)
=>
{
if
(
err
===
undefined
)
{
console
.
info
(
'
album deleteAssets successfully
'
);
}
else
{
console
.
error
(
'
album deleteAssets failed with error:
'
+
err
);
}
});
}
catch
(
err
)
{
console
.
error
(
'
deleteAssetsDemoCallback failed with error:
'
+
err
);
}
}
```
### deleteAssets
deleteAssets(assets: Array
<
PhotoAsset
>
): Promise
<
void
>
;
从回收站中彻底删除图片或者视频,需要先在回收站中预置文件资源。该方法使用Promise来返回结果。
**注意**
:此操作不可逆,执行此操作后文件资源将彻底删除,请谨慎操作。
**系统接口**
:此接口为系统接口。
**需要权限**
:ohos.permission.WRITE_IMAGEVIDEO
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ---------- |
| assets | Array
<
[PhotoAsset](#photoasset)
>
| 是 | 回收站中待彻底删除图片或者视频数组。 |
**返回值:**
| 类型 | 说明 |
| --------------------------------------- | ----------------- |
|Promise
<
void
>
| Promise对象,返回void。 |
**错误码:**
接口抛出错误码的详细介绍请参见
[
通用错误码
](
../errorcodes/errorcode-universal.md
)
。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 202 | Called by non-system application. |
| 401 | if PhotoAssets is invalid. |
**示例:**
```
ts
import
dataSharePredicates
from
'
@ohos.data.dataSharePredicates
'
;
async
function
example
()
{
try
{
console
.
info
(
'
deleteAssetsDemoPromise
'
);
let
predicates
=
new
dataSharePredicates
.
DataSharePredicates
();
let
fetchOption
=
{
fetchColumns
:
[],
predicates
:
predicates
};
let
albumFetchResult
=
await
phAccessHelper
.
getAlbums
(
photoAccessHelper
.
AlbumType
.
SYSTEM
,
photoAccessHelper
.
AlbumSubtype
.
TRASH
);
let
album
=
await
albumFetchResult
.
getFirstObject
();
let
fetchResult
=
await
album
.
getAssets
(
fetchOption
);
let
asset
=
await
fetchResult
.
getFirstObject
();
album
.
deleteAssets
([
asset
]).
then
(()
=>
{
console
.
info
(
'
album deleteAssets successfully
'
);
}).
catch
((
err
)
=>
{
console
.
error
(
'
album deleteAssets failed with error:
'
+
err
);
});
}
catch
(
err
)
{
console
.
error
(
'
deleteAssetsDemoPromise failed with error:
'
+
err
);
}
}
```
## MemberType
成员类型。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
| 名称 | 类型 | 可读 | 可写 | 说明 |
| ----- | ---- | ---- | ---- | ---- |
| number | number | 是 | 是 | number类型。 |
| string | string | 是 | 是 | string类型。|
| boolean | boolean | 是 | 是 | boolean类型。 |
## PhotoType
枚举,媒体文件类型。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
| 名称 | 值 | 说明 |
| ----- | ---- | ---- |
| IMAGE | 1 | 图片。 |
| VIDEO | 2 | 视频。 |
## PhotoSubtype
枚举,不同
[
PhotoAsset
](
#photoasset
)
的类型。
**系统接口**
:此接口为系统接口。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
| 名称 | 值 | 说明 |
| ----- | ---- | ---- |
| DEFAULT | 0 | 默认照片类型。 |
| SCREENSHOT | 1 | 截屏录屏文件类型。 |
## PositionType
枚举,文件位置,表示文件在本地或云端。
**系统接口**
:此接口为系统接口。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
| 名称 | 值 | 说明 |
| ----- | ---- | ---- |
| LOCAL | 1 << 0 | 文件只存在于本端设备。 |
| CLOUD | 1 << 1 | 文件只存在于云端。 |
## AlbumType
枚举,相册类型,表示是用户相册还是系统预置相册。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
| 名称 | 值 | 说明 |
| ----- | ---- | ---- |
| USER | 0 | 用户相册。 |
| SYSTEM | 1024 | 系统预置相册。 |
## AlbumSubtype
枚举,相册子类型,表示具体的相册类型。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
| 名称 | 值 | 说明 |
| ----- | ---- | ---- |
| USER_GENERIC | 1 | 用户相册。 |
| FAVORITE | 1025 | 收藏夹。 |
| VIDEO | 1026 | 视频相册。 |
| HIDDEN | 1027 | 隐藏相册。
**系统接口**
:此接口为系统接口。 |
| TRASH | 1028 | 回收站。
**系统接口**
:此接口为系统接口。 |
| SCREENSHOT | 1029 | 截屏和录屏相册。
**系统接口**
:此接口为系统接口。 |
| CAMERA | 1030 | 相机拍摄的照片和视频相册。
**系统接口**
:此接口为系统接口。 |
| ANY | 2147483647 | 任意相册。 |
## PhotoKeys
枚举,图片和视频文件关键信息。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
| 名称 | 值 | 说明 |
| ------------- | ------------------- | ---------------------------------------------------------- |
| URI | 'uri' | 文件uri。 |
| PHOTO_TYPE | 'media_type' | 媒体文件类型。 |
| DISPLAY_NAME | 'display_name' | 显示名字。 |
| SIZE | 'size' | 文件大小。 |
| DATE_ADDED | 'date_added' | 添加日期(添加文件时间距1970年1月1日的秒数值)。 |
| DATE_MODIFIED | 'date_modified' | 修改日期(修改文件时间距1970年1月1日的秒数值,修改文件名不会改变此值,当文件内容发生修改时才会更新)。 |
| DURATION | 'duration' | 持续时间(单位:毫秒)。 |
| WIDTH | 'width' | 图片宽度(单位:像素)。 |
| HEIGHT | 'height' | 图片高度(单位:像素)。 |
| DATE_TAKEN | 'date_taken' | 拍摄日期(文件拍照时间距1970年1月1日的秒数值)。 |
| ORIENTATION | 'orientation' | 图片文件的方向。 |
| FAVORITE | 'is_favorite' | 收藏。 |
| TITLE | 'title' | 文件标题。 |
| POSITION | 'position' | 文件位置类型。
**系统接口**
:此接口为系统接口。 |
| DATE_TRASHED | 'date_trashed' | 删除日期(删除文件时间距1970年1月1日的秒数值)。
**系统接口**
:此接口为系统接口。 |
| HIDDEN | 'hidden' | 文件的隐藏状态。
**系统接口**
:此接口为系统接口。 |
## AlbumKeys
枚举,相册关键信息。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
| 名称 | 值 | 说明 |
| ------------- | ------------------- | ---------------------------------------------------------- |
| URI | 'uri' | 相册uri。 |
| ALBUM_NAME | 'album_name' | 相册名字。 |
## PhotoCreateOptions
图片或视频的创建选项。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
| 名称 | 类型 | 必填 | 说明 |
| ---------------------- | ------------------- | ---- | ------------------------------------------------ |
| subtype |
[
PhotoSubtype
](
#photosubtype
)
| 否 | 图片或者视频的子类型。
**系统接口**
:此接口为系统接口。 |
## CreateOptions
图片或视频的创建选项。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
| 名称 | 类型 | 必填 | 说明 |
| ---------------------- | ------------------- | ---- | ------------------------------------------------ |
| title | string | 否 | 图片或者视频的标题。 |
## FetchOptions
检索条件。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
| 名称 | 类型 | 可读 | 可写 | 说明 |
| ---------------------- | ------------------- | ---- |---- | ------------------------------------------------ |
| fetchColumns | Array
<
string
>
| 是 | 是 | 检索条件,指定列名查询,如果该参数为空时默认查询uri、name、photoType(具体字段名称以检索对象定义为准)。示例:
<br
/>
fetchColumns: ['uri', 'title']。 |
| predicates |
[
dataSharePredicates.DataSharePredicates
](
js-apis-data-dataSharePredicates.md
)
| 是 | 是 | 谓词查询,显示过滤条件。 |
## ChangeData
监听器回调函数的值。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
| 名称 | 类型 | 可读 | 可写 | 说明 |
| ------- | --------------------------- | ---- | ---- | ------------------------------------------------------------ |
| type |
[
NotifyType
](
#notifytype
)
| 是 | 否 | ChangeData的通知类型。 |
| uris | Array
<
string
>
| 是 | 否 | 相同
[
NotifyType
](
#notifytype
)
的所有uri,可以是PhotoAsset或Album。 |
| extraUris | Array
<
string
>
| 是 | 否 | 相册中变动文件的uri数组。 |
## NotifyType
枚举,通知事件的类型。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
| 名称 | 值 | 说明 |
| ------------------------- | ---- | -------------------------------- |
| NOTIFY_ADD | 0 | 添加文件集或相册通知的类型。 |
| NOTIFY_UPDATE | 1 | 文件集或相册的更新通知类型。 |
| NOTIFY_REMOVE | 2 | 删除文件集或相册的通知类型。 |
| NOTIFY_ALBUM_ADD_ASSET | 3 | 在相册中添加的文件集的通知类型。 |
| NOTIFY_ALBUM_REMOVE_ASSET | 4 | 在相册中删除的文件集的通知类型。 |
## DefaultChangeUri
枚举,DefaultChangeUri子类型。
**系统能力**
:SystemCapability.FileManagement.PhotoAccessHelper.Core
| 名称 | 值 | 说明 |
| ----------------- | ----------------------- | ------------------------------------------------------------ |
| DEFAULT_PHOTO_URI | 'file://media/Photo' | 默认PhotoAsset的Uri,与forSubUri{true}一起使用,将接收所有PhotoAsset的更改通知。 |
| DEFAULT_ALBUM_URI | 'file://media/PhotoAlbum' | 默认相册的Uri,与forSubUri{true}一起使用,将接收所有相册的更改通知。 |
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录