未验证 提交 920417ce 编写于 作者: O openharmony_ci 提交者: Gitee

!23892 媒体库开发指南ArkTs适配修改

Merge pull request !23892 from 胡伟奇/ArkTs_update
......@@ -33,29 +33,27 @@ photoAccessHelper提供监听媒体资源变更的接口,供开发者对指定
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.ImageVideoKey.DISPLAY_NAME, 'test.jpg');
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
console.info('getAssets fileAsset.uri : ' + fileAsset.uri);
let onCallback = (changeData:dataSharePredicates.ChangeData) => {
console.info('onCallback successfully, changData: ' + JSON.stringify(changeData));
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg');
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
console.info('getAssets fileAsset.uri : ' + fileAsset.uri);
let onCallback = (changeData: photoAccessHelper.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 {
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKey.ALBUM_NAME;
predicates.equalTo(albumName, 'albumName');
let fetchOptions: dataSharePredicates.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
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));
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
predicates.equalTo(albumName, 'albumName');
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
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));
}
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 {
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let onCallback = (changeData: dataSharePredicates.ChangeData) => {
console.info('onCallback successfully, changData: ' + JSON.stringify(changeData));
}
phAccessHelper.registerChange(photoAccessHelper.DefaultChangeUri.DEFAULT_PHOTO_URI, true, onCallback);
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = 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);
async function example() {
let onCallback = (changeData: photoAccessHelper.ChangeData) => {
console.info('onCallback successfully, changData: ' + JSON.stringify(changeData));
}
phAccessHelper.registerChange(photoAccessHelper.DefaultChangeUri.DEFAULT_PHOTO_URI, true, onCallback);
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
console.info('getAssets fileAsset.uri : ' + fileAsset.uri);
await fileAsset.setFavorite(true);
fetchResult.close();
} catch (err) {
console.error('onCallback failed with err: ' + err);
}
}
```
......@@ -172,33 +169,31 @@ try {
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.ImageVideoKey.DISPLAY_NAME, 'test.jpg');
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
console.info('getAssets fileAsset.uri : ' + fileAsset.uri);
let onCallback1 = (changeData: photoAccessHelper.ChangeData) => {
console.info('onCallback1, changData: ' + JSON.stringify(changeData));
}
let onCallback2 = (changeData: photoAccessHelper.ChangeData) => {
console.info('onCallback2, changData: ' + JSON.stringify(changeData));
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg');
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
console.info('getAssets fileAsset.uri : ' + fileAsset.uri);
let onCallback1 = (changeData: photoAccessHelper.ChangeData) => {
console.info('onCallback1, 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
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.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));
console.info('permissionRequestResult: ' + JSON.stringify(result));
}
});
}
......
......@@ -27,34 +27,24 @@
下面以查询文件名为'test.jpg'的图片资源为例。
**开发步骤:**
创建FetchOptions对象指定检索条件为检索文件名为'test.jpg'的图片。
```ts
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接口获取图片资源。
```ts
import photoAccessHelper from '@ohos.file.photoAccessHelper';
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
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);
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg');
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
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);
}
}
```
......@@ -64,65 +54,53 @@ try {
```ts
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
import photoAccessHelper from '@ohos.file.photoAccessHelper';
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
console.info('getAssets fileAsset.uri : ' + fileAsset.uri);
fetchResult.close();
} catch (err) {
console.error('getAssets failed with err: ' + err);
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = 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';
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
import photoAccessHelper from '@ohos.file.photoAccessHelper';
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);
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = 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 = photoAccessHelper.PhotoKeys.DATE_ADDED;
predicates.between(date_added, startTime, endTime);
predicates.orderByDesc(date_added); // 查询结果按照降序排序。
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [date_added], // date_added属性不属于默认查询列,需要自行添加。
predicates: predicates
};
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 {
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
import image from '@ohos.multimedia.image';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName);
let size: image.Size = { width: 720, height: 720 };
let pixelMap: image.PixelMap = await fileAsset.getThumbnail(size);
let imageInfo: image.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);
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName);
let size: image.Size = { width: 720, height: 720 };
let pixelMap: image.PixelMap = await fileAsset.getThumbnail(size);
let imageInfo: image.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);
}
}
```
......@@ -193,18 +172,18 @@ try {
2. 调用createAsset接口创建图片资源。
```ts
import photoAccessHelper from '@ohos.file.photoAccessHelper';
try {
let displayName: string = 'testPhoto' + Date.now() + '.jpg';
let createOption: photoAccessHelper.CreateOptions = {
subType: photoAccessHelper.PhotoSubtype.DEFAULT
};
let fileAsset: photoAccessHelper.photoAsset = await phAccessHelper.createAsset(displayName, createOption);
console.info('createAsset successfully, file displayName: ' + fileAsset.displayName);
} catch (err) {
console.error('createAsset failed, message = ', err);
async function example() {
try {
let displayName: string = 'testPhoto' + Date.now() + '.jpg';
let createOption: photoAccessHelper.PhotoCreateOptions = {
subtype: photoAccessHelper.PhotoSubtype.DEFAULT
};
let fileAsset: photoAccessHelper.PhotoAsset = await phAccessHelper.createAsset(displayName, createOption);
console.info('createAsset successfully, file displayName: ' + fileAsset.displayName);
} catch (err) {
console.error('createAsset failed, message = ', err);
}
}
```
......@@ -233,26 +212,27 @@ try {
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: ['title'],
predicates: predicates
};
let newTitle = 'newTestPhoto';
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
let title: photoAccessHelper.PhotoKeys = photoAccessHelper.PhotoKeys.TITLE;
let fileAssetTitle: photoAccessHelper.MemberType = 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);
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: ['title'],
predicates: predicates
};
let newTitle = 'newTestPhoto';
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
let title: photoAccessHelper.PhotoKeys = photoAccessHelper.PhotoKeys.TITLE;
let fileAssetTitle: photoAccessHelper.MemberType = 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);
}
}
```
......@@ -278,21 +258,22 @@ try {
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = 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);
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = 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);
}
}
```
......@@ -29,15 +29,15 @@ photoAccessHelper仅提供开发者对收藏夹、视频相册、截屏和录屏
2. 调用getAlbums接口获取收藏夹对象。
```ts
import photoAccessHelper from '@ohos.file.photoAccessHelper';
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE);
let album: photoAccessHelper.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);
async function example() {
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE);
let album: photoAccessHelper.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);
}
}
```
......@@ -60,23 +60,24 @@ try {
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.ImageVideoKey.DISPLAY_NAME, 'test.jpg');
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject();
console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName);
let favoriteState = true;
await fileAsset.setFavorite(favoriteState);
} catch (err) {
console.error('setFavorite failed with err: ' + err);
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg');
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject();
console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName);
let favoriteState = true;
await fileAsset.setFavorite(favoriteState);
} catch (err) {
console.error('setFavorite failed with err: ' + err);
}
}
```
......@@ -100,26 +101,27 @@ try {
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE);
let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
console.info('get favorite Album successfully, albumUri: ' + album.albumUri);
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = 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);
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE);
let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
console.info('get favorite Album successfully, albumUri: ' + album.albumUri);
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = 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);
}
}
```
......@@ -143,28 +145,29 @@ try {
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE);
let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
console.info('get favorite Album successfully, albumUri: ' + album.albumUri);
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject();
console.info('favorite album getAssets successfully, albumName: ' + fileAsset.displayName);
let favoriteState = false;
await fileAsset.setFavorite(favoriteState);
photoFetchResult.close();
albumFetchResult.close();
} catch (err) {
console.error('setFavorite failed with err: ' + err);
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE);
let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
console.info('get favorite Album successfully, albumUri: ' + album.albumUri);
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject();
console.info('favorite album getAssets successfully, albumName: ' + fileAsset.displayName);
let favoriteState = false;
await fileAsset.setFavorite(favoriteState);
photoFetchResult.close();
albumFetchResult.close();
} catch (err) {
console.error('setFavorite failed with err: ' + err);
}
}
```
......@@ -187,15 +190,15 @@ try {
2. 调用getAlbums接口获取视频相册。
```ts
import photoAccessHelper from '@ohos.file.photoAccessHelper';
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.VIDEO);
let album: photoAccessHelper.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);
async function example() {
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.VIDEO);
let album: photoAccessHelper.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);
}
}
```
......@@ -219,26 +222,27 @@ try {
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.VIDEO);
let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
console.info('get video Album successfully, albumUri: ' + album.albumUri);
let videoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = 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);
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.VIDEO);
let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
console.info('get video Album successfully, albumUri: ' + album.albumUri);
let videoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = 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);
}
}
```
......@@ -261,15 +265,15 @@ try {
2. 调用getAlbums接口获取截屏和录屏相册。
```ts
import photoAccessHelper from '@ohos.file.photoAccessHelper';
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.SCREENSHOT);
let album: photoAccessHelper.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);
async function example() {
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.SCREENSHOT);
let album: photoAccessHelper.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);
}
}
```
......@@ -293,25 +297,26 @@ try {
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.SCREENSHOT);
let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
console.info('get screenshot album successfully, albumUri: ' + album.albumUri);
let screenshotFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = 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);
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.SCREENSHOT);
let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
console.info('get screenshot album successfully, albumUri: ' + album.albumUri);
let screenshotFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = 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);
}
}
```
......@@ -34,14 +34,14 @@ photoAccessHelper提供用户相册相关的接口,供开发者创建、删除
2. 调用createAlbum接口创建相册。
```ts
import photoAccessHelper from '@ohos.file.photoAccessHelper';
try {
let albumName = 'albumName';
let album: photoAccessHelper.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);
async function example() {
try {
let albumName = 'albumName';
let album: photoAccessHelper.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);
}
}
```
......@@ -64,23 +64,24 @@ try {
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.ALBUM_NAME = photoAccessHelper.AlbumKey.ALBUM_NAME;
predicates.equalTo(albumName, 'albumName');
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
console.info('getAlbums successfully, albumName: ' + album.albumName);
fetchResult.close();
} catch (err) {
console.error('getAlbums failed with err: ' + err);
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
predicates.equalTo(albumName, 'albumName');
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
console.info('getAlbums successfully, albumName: ' + album.albumName);
fetchResult.close();
} catch (err) {
console.error('getAlbums failed with err: ' + err);
}
}
```
......@@ -111,25 +112,26 @@ try {
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.AlbumKey.ALBUM_NAME = photoAccessHelper.AlbumKey.ALBUM_NAME;
predicates.equalTo(albumName, 'albumName');
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
let album: photoAccessHelper.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);
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
predicates.equalTo(albumName, 'albumName');
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
let album: photoAccessHelper.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);
}
}
```
......@@ -156,34 +158,35 @@ try {
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.AlbumKey.ALBUM_NAME = photoAccessHelper.AlbumKey.ALBUM_NAME;
albumPredicates.equalTo(albumName, 'albumName');
let albumFetchOptions: dataSharePredicates.FetchOptions = {
fetchColumns: [],
predicates: albumPredicates
};
let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let photoFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: photoPredicates
};
try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
console.info('getAlbums successfully, albumName: ' + album.albumName);
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(photoFetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = 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);
async function example() {
let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
albumPredicates.equalTo(albumName, 'albumName');
let albumFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: albumPredicates
};
let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let photoFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: photoPredicates
};
try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
console.info('getAlbums successfully, albumName: ' + album.albumName);
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(photoFetchOptions);
let fileAsset: photoAccessHelper.PhotoAsset = 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);
}
}
```
......@@ -209,33 +212,34 @@ try {
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName = photoAccessHelper.AlbumKey.ALBUM_NAME;
albumPredicates.equalTo(albumName, 'albumName');
let albumFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: albumPredicates
};
let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let photoFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: photoPredicates
};
try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
let album: photoAccessHelper.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);
async function example() {
let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
albumPredicates.equalTo(albumName, 'albumName');
let albumFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: albumPredicates
};
let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let photoFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: photoPredicates
};
try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
let album: photoAccessHelper.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);
}
}
```
......@@ -264,34 +268,35 @@ try {
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.AlbumKey.ALBUM_NAME = photoAccessHelper.AlbumKey.ALBUM_NAME;
albumPredicates.equalTo(albumName, 'albumName');
let albumFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: albumPredicates
};
let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let photoFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: photoPredicates
};
try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
let album: photoAccessHelper.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);
async function example() {
let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
albumPredicates.equalTo(albumName, 'albumName');
let albumFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: albumPredicates
};
let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let photoFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: photoPredicates
};
try {
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
let album: photoAccessHelper.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);
}
}
```
......@@ -315,23 +320,24 @@ try {
```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.AlbumKey.ALBUM_NAME = photoAccessHelper.AlbumKey.ALBUM_NAME;
predicates.equalTo(albumName, '%albumName%');
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
let album: photoAccessHelper.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);
async function example() {
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
predicates.equalTo(albumName, '%albumName%');
let fetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
try {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
let album: photoAccessHelper.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);
}
}
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册