提交 58fb77ae 编写于 作者: H huweiqi

file-management ArkTs update

Signed-off-by: Nhuweiqi <huweiqi1@huawei.com>
Change-Id: I573f5626a986fa4753c1694035de85c99e6610e4
上级 5b6bf995
...@@ -33,29 +33,27 @@ photoAccessHelper提供监听媒体资源变更的接口,供开发者对指定 ...@@ -33,29 +33,27 @@ photoAccessHelper提供监听媒体资源变更的接口,供开发者对指定
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); async function example() {
predicates.equalTo(photoAccessHelper.ImageVideoKey.DISPLAY_NAME, 'test.jpg'); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = { predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg');
fetchColumns: [], let fetchOptions: photoAccessHelper.FetchOptions = {
predicates: predicates fetchColumns: [],
}; predicates: predicates
};
try { try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
console.info('getAssets fileAsset.uri : ' + fileAsset.uri); console.info('getAssets fileAsset.uri : ' + fileAsset.uri);
let onCallback = (changeData: photoAccessHelper.ChangeData) => {
let onCallback = (changeData:dataSharePredicates.ChangeData) => { console.info('onCallback successfully, changData: ' + JSON.stringify(changeData));
console.info('onCallback successfully, changData: ' + JSON.stringify(changeData)); }
phAccessHelper.registerChange(fileAsset.uri, false, onCallback);
await fileAsset.setFavorite(true);
fetchResult.close();
} catch (err) {
console.error('onCallback failed with err: ' + err);
} }
phAccessHelper.registerChange(fileAsset.uri, false, onCallback);
await fileAsset.favorite(true);
fetchResult.close();
} catch (err) {
console.error('onCallback failed with err: ' + err);
} }
``` ```
...@@ -78,31 +76,31 @@ try { ...@@ -78,31 +76,31 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); async function example() {
let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKey.ALBUM_NAME; let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(albumName, 'albumName'); let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
let fetchOptions: dataSharePredicates.FetchOptions = { predicates.equalTo(albumName, 'albumName');
fetchColumns: [], let fetchOptions: photoAccessHelper.FetchOptions = {
predicates: predicates fetchColumns: [],
}; predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); try {
let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
console.info('getAlbums successfullyfully, albumName: ' + album.albumUri); let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
console.info('getAlbums successfullyfully, albumName: ' + album.albumUri);
let onCallback = (changeData: photoAccessHelper.ChangeData) => {
console.info('onCallback successfully, changData: ' + JSON.stringify(changeData)); let onCallback = (changeData: photoAccessHelper.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);
} }
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);
} }
``` ```
...@@ -129,27 +127,26 @@ try { ...@@ -129,27 +127,26 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let onCallback = (changeData: dataSharePredicates.ChangeData) => { async function example() {
console.info('onCallback successfully, changData: ' + JSON.stringify(changeData)); let onCallback = (changeData: photoAccessHelper.ChangeData) => {
} console.info('onCallback successfully, changData: ' + JSON.stringify(changeData));
phAccessHelper.registerChange(photoAccessHelper.DefaultChangeUri.DEFAULT_PHOTO_URI, true, onCallback); }
phAccessHelper.registerChange(photoAccessHelper.DefaultChangeUri.DEFAULT_PHOTO_URI, true, onCallback);
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = { let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [], fetchColumns: [],
predicates: predicates predicates: predicates
}; };
try {
try { let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); console.info('getAssets fileAsset.uri : ' + fileAsset.uri);
console.info('getAssets fileAsset.uri : ' + fileAsset.uri); await fileAsset.setFavorite(true);
await fileAsset.favorite(true); fetchResult.close();
fetchResult.close(); } catch (err) {
} catch (err) { console.error('onCallback failed with err: ' + err);
console.error('onCallback failed with err: ' + err); }
} }
``` ```
...@@ -172,33 +169,31 @@ try { ...@@ -172,33 +169,31 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.ImageVideoKey.DISPLAY_NAME, 'test.jpg'); predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg');
let fetchOptions: photoAccessHelper.FetchOptions = { let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [], fetchColumns: [],
predicates: predicates predicates: predicates
}; };
try {
try { let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); console.info('getAssets fileAsset.uri : ' + fileAsset.uri);
console.info('getAssets fileAsset.uri : ' + fileAsset.uri); let onCallback1 = (changeData: photoAccessHelper.ChangeData) => {
console.info('onCallback1, changData: ' + JSON.stringify(changeData));
let onCallback1 = (changeData: photoAccessHelper.ChangeData) => { }
console.info('onCallback1, changData: ' + JSON.stringify(changeData)); let onCallback2 = (changeData: photoAccessHelper.ChangeData) => {
} console.info('onCallback2, changData: ' + JSON.stringify(changeData));
let onCallback2 = (changeData: photoAccessHelper.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.setFavorite(true);
fetchResult.close();
} catch (err) {
console.error('onCallback failed with err: ' + err);
} }
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);
} }
``` ```
...@@ -107,14 +107,12 @@ let phAccessHelper: photoAccessHelper.PhotoAccessHelper = photoAccessHelper.getP ...@@ -107,14 +107,12 @@ let phAccessHelper: photoAccessHelper.PhotoAccessHelper = photoAccessHelper.getP
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage) {
let list : Array<Permissions> = ['ohos.permission.READ_IMAGEVIDEO', 'ohos.permission.WRITE_IMAGEVIDEO']; let list : Array<Permissions> = ['ohos.permission.READ_IMAGEVIDEO', 'ohos.permission.WRITE_IMAGEVIDEO'];
let permissionRequestResult;
let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
atManager.requestPermissionsFromUser(this.context, list, (err, result) => { atManager.requestPermissionsFromUser(this.context, list, (err, result) => {
if (err) { if (err) {
console.error('requestPermissionsFromUserError: ' + JSON.stringify(err)); console.error('requestPermissionsFromUserError: ' + JSON.stringify(err));
} else { } else {
permissionRequestResult = result; console.info('permissionRequestResult: ' + JSON.stringify(result));
console.info('permissionRequestResult: ' + JSON.stringify(permissionRequestResult));
} }
}); });
} }
......
...@@ -27,34 +27,24 @@ ...@@ -27,34 +27,24 @@
下面以查询文件名为'test.jpg'的图片资源为例。 下面以查询文件名为'test.jpg'的图片资源为例。
**开发步骤:**
创建FetchOptions对象指定检索条件为检索文件名为'test.jpg'的图片。
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg');
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
```
调用PhotoAccessHelper.getAssets接口获取图片资源。 async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
```ts predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg');
import photoAccessHelper from '@ohos.file.photoAccessHelper'; let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
try { predicates: predicates
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); };
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); try {
console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName); let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
fetchResult.close(); let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
} catch (err) { console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName);
console.error('getAssets failed with err: ' + err); fetchResult.close();
} catch (err) {
console.error('getAssets failed with err: ' + err);
}
} }
``` ```
...@@ -64,65 +54,53 @@ try { ...@@ -64,65 +54,53 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.PhotoKeys.URI, 'file://media/Photo/1');
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
```
调用PhotoAccessHelper.getAssets接口获取图片资源。 async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.PhotoKeys.URI, 'file://media/Photo/1');
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
```ts try {
import photoAccessHelper from '@ohos.file.photoAccessHelper'; let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
try { console.info('getAssets fileAsset.uri : ' + fileAsset.uri);
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); fetchResult.close();
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); } catch (err) {
console.info('getAssets fileAsset.uri : ' + fileAsset.uri); console.error('getAssets failed with err: ' + err);
fetchResult.close(); }
} catch (err) {
console.error('getAssets failed with err: ' + err);
} }
``` ```
### 指定文件添加的时间获取图片或视频资源 ### 指定文件添加的时间获取图片或视频资源
下面以查询指定添加时间为'2022-06-01'至'2023-06-01'这一年内为例。 下面以查询指定添加时间为'2022-06-01'至'2023-06-01'这一年内为例。
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let startTime: Number = Date.parse(new Date('2022-06-01').toString()) / 1000; // The value of the start time is the number of seconds elapsed since the Epoch time.
let endTime: Number = Date.parse(new Date('2023-06-01').toString()) / 1000; // The value of the end time is the number of seconds elapsed since the Epoch time.
let date_added: photoAccessHelper.PhotoKeys = photoAccessHelper.PhotoKeys.DATE_ADDED;
predicates.between(date_added, startTime, endTime);
predicates.orderByDesc(date_added); // Sort the obtained records in descending order.
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [date_added], // The date_added attribute is not a default option and needs to be added.
predicates: predicates
};
```
调用PhotoAccessHelper.getAssets接口获取图片资源。
```ts async function example() {
import photoAccessHelper from '@ohos.file.photoAccessHelper'; let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let startTime = Date.parse(new Date('2022-06-01').toString()) / 1000; // 查询起始时间距1970年1月1日的秒数值。
try { let endTime = Date.parse(new Date('2023-06-01').toString()) / 1000; // 查询结束时间距1970年1月1日的秒数值。
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); let date_added: photoAccessHelper.PhotoKeys = photoAccessHelper.PhotoKeys.DATE_ADDED;
console.info('getAssets count: ' + fetchResult.getCount()); predicates.between(date_added, startTime, endTime);
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); predicates.orderByDesc(date_added); // 查询结果按照降序排序。
console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName); let fetchOptions: photoAccessHelper.FetchOptions = {
fetchResult.close(); fetchColumns: [date_added], // date_added属性不属于默认查询列,需要自行添加。
} catch (err) { predicates: predicates
console.error('getAssets failed with err: ' + err); };
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
console.info('getAssets count: ' + fetchResult.getCount());
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName);
fetchResult.close();
} catch (err) {
console.error('getAssets failed with err: ' + err);
}
} }
``` ```
...@@ -151,26 +129,27 @@ try { ...@@ -151,26 +129,27 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
import image from '@ohos.multimedia.image'; import image from '@ohos.multimedia.image';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); async function example() {
let fetchOptions: photoAccessHelper.FetchOptions = { let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
fetchColumns: [], let fetchOptions: photoAccessHelper.FetchOptions = {
predicates: predicates fetchColumns: [],
}; predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); try {
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName); let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
let size: image.Size = { width: 720, height: 720 }; console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName);
let pixelMap: image.PixelMap = await fileAsset.getThumbnail(size); let size: image.Size = { width: 720, height: 720 };
let imageInfo: image.ImageInfo = await pixelMap.getImageInfo() let pixelMap: image.PixelMap = await fileAsset.getThumbnail(size);
console.info('getThumbnail successful, pixelMap ImageInfo size: ' + JSON.stringify(imageInfo.size)); let imageInfo: image.ImageInfo = await pixelMap.getImageInfo()
fetchResult.close(); console.info('getThumbnail successful, pixelMap ImageInfo size: ' + JSON.stringify(imageInfo.size));
} catch (err) { fetchResult.close();
console.error('getThumbnail failed with err: ' + err); } catch (err) {
console.error('getThumbnail failed with err: ' + err);
}
} }
``` ```
...@@ -193,18 +172,18 @@ try { ...@@ -193,18 +172,18 @@ try {
2. 调用createAsset接口创建图片资源。 2. 调用createAsset接口创建图片资源。
```ts ```ts
import photoAccessHelper from '@ohos.file.photoAccessHelper'; async function example() {
try {
try { let displayName: string = 'testPhoto' + Date.now() + '.jpg';
let displayName: string = 'testPhoto' + Date.now() + '.jpg'; let createOption: photoAccessHelper.PhotoCreateOptions = {
let createOption: photoAccessHelper.CreateOptions = { subtype: photoAccessHelper.PhotoSubtype.DEFAULT
subType: photoAccessHelper.PhotoSubtype.DEFAULT };
};
let fileAsset: photoAccessHelper.PhotoAsset = await phAccessHelper.createAsset(displayName, createOption);
let fileAsset: photoAccessHelper.photoAsset = await phAccessHelper.createAsset(displayName, createOption); console.info('createAsset successfully, file displayName: ' + fileAsset.displayName);
console.info('createAsset successfully, file displayName: ' + fileAsset.displayName); } catch (err) {
} catch (err) { console.error('createAsset failed, message = ', err);
console.error('createAsset failed, message = ', err); }
} }
``` ```
...@@ -233,26 +212,27 @@ try { ...@@ -233,26 +212,27 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = { let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: ['title'], fetchColumns: ['title'],
predicates: predicates predicates: predicates
}; };
let newTitle = 'newTestPhoto'; let newTitle = 'newTestPhoto';
try { try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
let title: photoAccessHelper.PhotoKeys = photoAccessHelper.PhotoKeys.TITLE; let title: photoAccessHelper.PhotoKeys = photoAccessHelper.PhotoKeys.TITLE;
let fileAssetTitle: photoAccessHelper.MemberType = fileAsset.get(title); let fileAssetTitle: photoAccessHelper.MemberType = fileAsset.get(title);
console.info('getAssets fileAsset.title : ' + fileAssetTitle); console.info('getAssets fileAsset.title : ' + fileAssetTitle);
fileAsset.set(title, newTitle); fileAsset.set(title, newTitle);
await fileAsset.commitModify(); await fileAsset.commitModify();
fetchResult.close(); fetchResult.close();
} catch (err) { } catch (err) {
console.error('commitModify failed with err: ' + err); console.error('commitModify failed with err: ' + err);
}
} }
``` ```
...@@ -278,21 +258,22 @@ try { ...@@ -278,21 +258,22 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = { let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [], fetchColumns: [],
predicates: predicates predicates: predicates
}; };
try { try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
console.info('getAssets fileAsset.uri : ' + fileAsset.uri); console.info('getAssets fileAsset.uri : ' + fileAsset.uri);
await phAccessHelper.deleteAssets([fileAsset.uri]); await phAccessHelper.deleteAssets([fileAsset.uri]);
fetchResult.close(); fetchResult.close();
} catch (err) { } catch (err) {
console.error('deleteAssets failed with err: ' + err); console.error('deleteAssets failed with err: ' + err);
}
} }
``` ```
...@@ -29,15 +29,15 @@ photoAccessHelper仅提供开发者对收藏夹、视频相册、截屏和录屏 ...@@ -29,15 +29,15 @@ photoAccessHelper仅提供开发者对收藏夹、视频相册、截屏和录屏
2. 调用getAlbums接口获取收藏夹对象。 2. 调用getAlbums接口获取收藏夹对象。
```ts ```ts
import photoAccessHelper from '@ohos.file.photoAccessHelper'; async function example() {
try {
try { let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE);
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE); let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); console.info('get favorite Album successfully, albumUri: ' + album.albumUri);
console.info('get favorite Album successfully, albumUri: ' + album.albumUri); fetchResult.close();
fetchResult.close(); } catch (err) {
} catch (err) { console.error('get favorite Album failed with err: ' + err);
console.error('get favorite Album failed with err: ' + err); }
} }
``` ```
...@@ -60,23 +60,24 @@ try { ...@@ -60,23 +60,24 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.ImageVideoKey.DISPLAY_NAME, 'test.jpg'); predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg');
let fetchOptions: photoAccessHelper.FetchOptions = { let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [], fetchColumns: [],
predicates: predicates predicates: predicates
}; };
try { try {
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject(); let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject();
console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName); console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName);
let favoriteState = true; let favoriteState = true;
await fileAsset.setFavorite(favoriteState); await fileAsset.setFavorite(favoriteState);
} catch (err) { } catch (err) {
console.error('setFavorite failed with err: ' + err); console.error('setFavorite failed with err: ' + err);
}
} }
``` ```
...@@ -100,26 +101,27 @@ try { ...@@ -100,26 +101,27 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = { let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [], fetchColumns: [],
predicates: predicates predicates: predicates
}; };
try { try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE); let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE);
let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
console.info('get favorite Album successfully, albumUri: ' + album.albumUri); console.info('get favorite Album successfully, albumUri: ' + album.albumUri);
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions); let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject(); let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject();
console.info('favorite album getAssets successfully, albumName: ' + fileAsset.displayName); console.info('favorite album getAssets successfully, albumName: ' + fileAsset.displayName);
photoFetchResult.close(); photoFetchResult.close();
albumFetchResult.close(); albumFetchResult.close();
} catch (err) { } catch (err) {
console.error('favorite failed with err: ' + err); console.error('favorite failed with err: ' + err);
}
} }
``` ```
...@@ -143,28 +145,29 @@ try { ...@@ -143,28 +145,29 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = { let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [], fetchColumns: [],
predicates: predicates predicates: predicates
}; };
try { try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE); let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE);
let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
console.info('get favorite Album successfully, albumUri: ' + album.albumUri); console.info('get favorite Album successfully, albumUri: ' + album.albumUri);
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions); let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject(); let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject();
console.info('favorite album getAssets successfully, albumName: ' + fileAsset.displayName); console.info('favorite album getAssets successfully, albumName: ' + fileAsset.displayName);
let favoriteState = false; let favoriteState = false;
await fileAsset.setFavorite(favoriteState); await fileAsset.setFavorite(favoriteState);
photoFetchResult.close(); photoFetchResult.close();
albumFetchResult.close(); albumFetchResult.close();
} catch (err) { } catch (err) {
console.error('setFavorite failed with err: ' + err); console.error('setFavorite failed with err: ' + err);
}
} }
``` ```
...@@ -187,15 +190,15 @@ try { ...@@ -187,15 +190,15 @@ try {
2. 调用getAlbums接口获取视频相册。 2. 调用getAlbums接口获取视频相册。
```ts ```ts
import photoAccessHelper from '@ohos.file.photoAccessHelper'; async function example() {
try {
try { let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.VIDEO);
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.VIDEO); let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); console.info('get video Album successfully, albumUri: ' + album.albumUri);
console.info('get video Album successfully, albumUri: ' + album.albumUri); fetchResult.close();
fetchResult.close(); } catch (err) {
} catch (err) { console.error('get video Album failed with err: ' + err);
console.error('get video Album failed with err: ' + err); }
} }
``` ```
...@@ -219,26 +222,27 @@ try { ...@@ -219,26 +222,27 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = { let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [], fetchColumns: [],
predicates: predicates predicates: predicates
}; };
try { try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.VIDEO); let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.VIDEO);
let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
console.info('get video Album successfully, albumUri: ' + album.albumUri); console.info('get video Album successfully, albumUri: ' + album.albumUri);
let videoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions); let videoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await videoFetchResult.getFirstObject(); let fileAsset: photoAccessHelper.PhotoAsset = await videoFetchResult.getFirstObject();
console.info('video album getAssets successfully, albumName: ' + fileAsset.displayName); console.info('video album getAssets successfully, albumName: ' + fileAsset.displayName);
videoFetchResult.close(); videoFetchResult.close();
albumFetchResult.close(); albumFetchResult.close();
} catch (err) { } catch (err) {
console.error('video failed with err: ' + err); console.error('video failed with err: ' + err);
}
} }
``` ```
...@@ -261,15 +265,15 @@ try { ...@@ -261,15 +265,15 @@ try {
2. 调用getAlbums接口获取截屏和录屏相册。 2. 调用getAlbums接口获取截屏和录屏相册。
```ts ```ts
import photoAccessHelper from '@ohos.file.photoAccessHelper'; async function example() {
try {
try { let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.SCREENSHOT);
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.SCREENSHOT); let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); console.info('get screenshot Album successfully, albumUri: ' + album.albumUri);
console.info('get screenshot Album successfully, albumUri: ' + album.albumUri); fetchResult.close();
fetchResult.close(); } catch (err) {
} catch (err) { console.error('get screenshot Album failed with err: ' + err);
console.error('get screenshot Album failed with err: ' + err); }
} }
``` ```
...@@ -293,25 +297,26 @@ try { ...@@ -293,25 +297,26 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = { let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [], fetchColumns: [],
predicates: predicates predicates: predicates
}; };
try { try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.SCREENSHOT); let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.SCREENSHOT);
let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
console.info('get screenshot album successfully, albumUri: ' + album.albumUri); console.info('get screenshot album successfully, albumUri: ' + album.albumUri);
let screenshotFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions); let screenshotFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await screenshotFetchResult.getFirstObject(); let fileAsset: photoAccessHelper.PhotoAsset = await screenshotFetchResult.getFirstObject();
console.info('screenshot album getAssets successfully, albumName: ' + fileAsset.displayName); console.info('screenshot album getAssets successfully, albumName: ' + fileAsset.displayName);
screenshotFetchResult.close(); screenshotFetchResult.close();
albumFetchResult.close(); albumFetchResult.close();
} catch (err) { } catch (err) {
console.error('screenshot album failed with err: ' + err); console.error('screenshot album failed with err: ' + err);
}
} }
``` ```
...@@ -34,14 +34,14 @@ photoAccessHelper提供用户相册相关的接口,供开发者创建、删除 ...@@ -34,14 +34,14 @@ photoAccessHelper提供用户相册相关的接口,供开发者创建、删除
2. 调用createAlbum接口创建相册。 2. 调用createAlbum接口创建相册。
```ts ```ts
import photoAccessHelper from '@ohos.file.photoAccessHelper'; async function example() {
try {
try { let albumName = 'albumName';
let albumName = 'albumName'; let album: photoAccessHelper.Album = await phAccessHelper.createAlbum(albumName);
let album: photoAccessHelper.Album = await phAccessHelper.createAlbum(albumName); console.info('createAlbum successfully, album: ' + album.albumName + ' album uri: ' + album.albumUri);
console.info('createAlbum successfully, album: ' + album.albumName + ' album uri: ' + album.albumUri); } catch (err) {
} catch (err) { console.error('createAlbum failed with err: ' + err);
console.error('createAlbum failed with err: ' + err); }
} }
``` ```
...@@ -64,23 +64,24 @@ try { ...@@ -64,23 +64,24 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.ALBUM_NAME = photoAccessHelper.AlbumKey.ALBUM_NAME; let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
predicates.equalTo(albumName, 'albumName'); predicates.equalTo(albumName, 'albumName');
let fetchOptions: photoAccessHelper.FetchOptions = { let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [], fetchColumns: [],
predicates: predicates predicates: predicates
}; };
try { try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
console.info('getAlbums successfully, albumName: ' + album.albumName); console.info('getAlbums successfully, albumName: ' + album.albumName);
fetchResult.close(); fetchResult.close();
} catch (err) { } catch (err) {
console.error('getAlbums failed with err: ' + err); console.error('getAlbums failed with err: ' + err);
}
} }
``` ```
...@@ -111,25 +112,26 @@ try { ...@@ -111,25 +112,26 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.AlbumKey.ALBUM_NAME = photoAccessHelper.AlbumKey.ALBUM_NAME; let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
predicates.equalTo(albumName, 'albumName'); predicates.equalTo(albumName, 'albumName');
let fetchOptions: photoAccessHelper.FetchOptions = { let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [], fetchColumns: [],
predicates: predicates predicates: predicates
}; };
try { try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
console.info('getAlbums successfully, albumName: ' + album.albumName); console.info('getAlbums successfully, albumName: ' + album.albumName);
album.albumName = 'newAlbumName'; album.albumName = 'newAlbumName';
await album.commitModify(); await album.commitModify();
fetchResult.close(); fetchResult.close();
} catch (err) { } catch (err) {
console.error('commitModify failed with err: ' + err); console.error('commitModify failed with err: ' + err);
}
} }
``` ```
...@@ -156,34 +158,35 @@ try { ...@@ -156,34 +158,35 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
async function example() {
let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.AlbumKey.ALBUM_NAME = photoAccessHelper.AlbumKey.ALBUM_NAME; let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
albumPredicates.equalTo(albumName, 'albumName'); albumPredicates.equalTo(albumName, 'albumName');
let albumFetchOptions: dataSharePredicates.FetchOptions = { let albumFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [], fetchColumns: [],
predicates: albumPredicates predicates: albumPredicates
}; };
let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let photoFetchOptions: photoAccessHelper.FetchOptions = { let photoFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [], fetchColumns: [],
predicates: photoPredicates predicates: photoPredicates
}; };
try { try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
console.info('getAlbums successfully, albumName: ' + album.albumName); console.info('getAlbums successfully, albumName: ' + album.albumName);
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(photoFetchOptions); let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(photoFetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject(); let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject();
console.info('getAssets successfully, albumName: ' + fileAsset.displayName); console.info('getAssets successfully, albumName: ' + fileAsset.displayName);
await album.addAssets([fileAsset]); await album.addAssets([fileAsset]);
albumFetchResult.close(); albumFetchResult.close();
photoFetchResult.close(); photoFetchResult.close();
} catch (err) { } catch (err) {
console.error('addAssets failed with err: ' + err); console.error('addAssets failed with err: ' + err);
}
} }
``` ```
...@@ -209,33 +212,34 @@ try { ...@@ -209,33 +212,34 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
async function example() {
let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName = photoAccessHelper.AlbumKey.ALBUM_NAME; let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
albumPredicates.equalTo(albumName, 'albumName'); albumPredicates.equalTo(albumName, 'albumName');
let albumFetchOptions: photoAccessHelper.FetchOptions = { let albumFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [], fetchColumns: [],
predicates: albumPredicates predicates: albumPredicates
}; };
let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let photoFetchOptions: photoAccessHelper.FetchOptions = { let photoFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [], fetchColumns: [],
predicates: photoPredicates predicates: photoPredicates
}; };
try { try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
console.info('getAlbums successfully, albumName: ' + album.albumName); console.info('getAlbums successfully, albumName: ' + album.albumName);
let photoFetchResult = await album.getAssets(photoFetchOptions); let photoFetchResult = await album.getAssets(photoFetchOptions);
let fileAsset = await photoFetchResult.getFirstObject(); let fileAsset = await photoFetchResult.getFirstObject();
console.info('album getAssets successfully, albumName: ' + fileAsset.displayName); console.info('album getAssets successfully, albumName: ' + fileAsset.displayName);
albumFetchResult.close(); albumFetchResult.close();
photoFetchResult.close(); photoFetchResult.close();
} catch (err) { } catch (err) {
console.error('album getAssets failed with err: ' + err); console.error('album getAssets failed with err: ' + err);
}
} }
``` ```
...@@ -264,34 +268,35 @@ try { ...@@ -264,34 +268,35 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
async function example() {
let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.AlbumKey.ALBUM_NAME = photoAccessHelper.AlbumKey.ALBUM_NAME; let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
albumPredicates.equalTo(albumName, 'albumName'); albumPredicates.equalTo(albumName, 'albumName');
let albumFetchOptions: photoAccessHelper.FetchOptions = { let albumFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [], fetchColumns: [],
predicates: albumPredicates predicates: albumPredicates
}; };
let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let photoFetchOptions: photoAccessHelper.FetchOptions = { let photoFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [], fetchColumns: [],
predicates: photoPredicates predicates: photoPredicates
}; };
try { try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
console.info('getAlbums successfully, albumName: ' + album.albumName); console.info('getAlbums successfully, albumName: ' + album.albumName);
let photoFetchResult = await album.getAssets(photoFetchOptions); let photoFetchResult = await album.getAssets(photoFetchOptions);
let fileAsset = await photoFetchResult.getFirstObject(); let fileAsset = await photoFetchResult.getFirstObject();
console.info('album getAssets successfully, albumName: ' + fileAsset.displayName); console.info('album getAssets successfully, albumName: ' + fileAsset.displayName);
await album.removeAssets([fileAsset]); await album.removeAssets([fileAsset]);
albumFetchResult.close(); albumFetchResult.close();
photoFetchResult.close(); photoFetchResult.close();
} catch (err) { } catch (err) {
console.error('removeAssets failed with err: ' + err); console.error('removeAssets failed with err: ' + err);
}
} }
``` ```
...@@ -315,23 +320,24 @@ try { ...@@ -315,23 +320,24 @@ try {
```ts ```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates'; import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.AlbumKey.ALBUM_NAME = photoAccessHelper.AlbumKey.ALBUM_NAME; let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
predicates.equalTo(albumName, '%albumName%'); predicates.equalTo(albumName, '%albumName%');
let fetchOptions: photoAccessHelper.FetchOptions = { let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [], fetchColumns: [],
predicates: predicates predicates: predicates
}; };
try { try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
console.info('getAlbums successfully, albumName: ' + album.albumName); console.info('getAlbums successfully, albumName: ' + album.albumName);
phAccessHelper.deleteAlbums([album]); phAccessHelper.deleteAlbums([album]);
fetchResult.close(); fetchResult.close();
} catch (err) { } catch (err) {
console.error('deleteAlbums failed with err: ' + err); console.error('deleteAlbums failed with err: ' + err);
}
} }
``` ```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册