377 lines
15 KiB
Plaintext
Raw Normal View History

2025-02-11 10:42:20 +08:00
import photoAccessHelper from '@ohos.file.photoAccessHelper';
import dataSharePredicates from '@ohos.data.dataSharePredicates';
import common from '@ohos.app.ability.common';
2025-03-26 10:37:10 +08:00
import { BusinessError } from '@ohos.base';
2024-05-09 13:42:56 +08:00
2025-01-20 08:50:40 +08:00
export class FileHelper {
2025-02-11 10:42:20 +08:00
FILE_ASSET_FETCH_COLUMNS = [
photoAccessHelper.PhotoKeys.URI,
2025-01-07 15:50:48 +08:00
photoAccessHelper.PhotoKeys.PHOTO_TYPE,
photoAccessHelper.PhotoKeys.DISPLAY_NAME,
photoAccessHelper.PhotoKeys.SIZE,
photoAccessHelper.PhotoKeys.DATE_ADDED,
photoAccessHelper.PhotoKeys.DATE_MODIFIED,
photoAccessHelper.PhotoKeys.DURATION,
photoAccessHelper.PhotoKeys.WIDTH,
photoAccessHelper.PhotoKeys.HEIGHT,
photoAccessHelper.PhotoKeys.DATE_TAKEN,
photoAccessHelper.PhotoKeys.ORIENTATION,
photoAccessHelper.PhotoKeys.FAVORITE,
photoAccessHelper.PhotoKeys.TITLE,
photoAccessHelper.PhotoKeys.POSITION,
photoAccessHelper.PhotoKeys.DATE_TRASHED,
2025-02-11 10:42:20 +08:00
photoAccessHelper.PhotoKeys.HIDDEN
];
private userFileMgr: photoAccessHelper.PhotoAccessHelper = undefined;
2024-05-09 13:42:56 +08:00
constructor() {
const context: common.UIAbilityContext = AppStorage.get('context')
2025-02-13 15:30:48 +08:00
this.userFileMgr = photoAccessHelper.getPhotoAccessHelper(context);
2024-05-09 13:42:56 +08:00
}
2025-03-26 10:37:10 +08:00
public async queryPhotoByDisplayName(displayName: string) {
2025-02-11 10:42:20 +08:00
console.log('baoyihu queryPhotoByDisplayName begin DISPLAY_NAME:' + displayName);
2024-05-09 13:42:56 +08:00
try {
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, displayName)
2025-03-26 10:37:10 +08:00
let fetchOptions: photoAccessHelper.FetchOptions = {
2024-05-09 13:42:56 +08:00
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
predicates: predicates
};
2025-01-20 08:50:40 +08:00
let assetsResult = await this.userFileMgr.getAssets(fetchOptions);
2024-05-09 13:42:56 +08:00
let retCount = assetsResult.getCount();
2025-02-11 10:42:20 +08:00
console.log('baoyihu queryPhotoByDisplayName count: ' + retCount);
2024-05-09 13:42:56 +08:00
if (retCount > 0) {
let asset = await assetsResult.getFirstObject();
2025-02-11 10:42:20 +08:00
console.log('baoyihu queryPhotoByDisplayName one asset uri : ' + asset.uri + ', photoType : ' +
asset.photoType + ', displayName : ' + asset.displayName);
console.log('baoyihu queryPhotoByDisplayName success ');
2024-05-09 13:42:56 +08:00
}
} catch (err) {
2025-02-11 10:42:20 +08:00
console.log('baoyihu queryPhotoByDisplayName failed with err: ' + err);
2024-05-09 13:42:56 +08:00
} finally {
}
}
async getUserAlbumItemByDisplayName(displayName: string): Promise<photoAccessHelper.Album> {
2025-01-20 08:50:40 +08:00
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null
2024-05-09 13:42:56 +08:00
let album: photoAccessHelper.Album = null
try {
2025-02-11 10:42:20 +08:00
console.log('getUserAlbumItemByDisplayName');
2024-05-09 13:42:56 +08:00
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.AlbumKeys.ALBUM_NAME, displayName)
2025-03-26 10:37:10 +08:00
let fetchOptions: photoAccessHelper.FetchOptions = {
2024-05-09 13:42:56 +08:00
fetchColumns: [],
predicates: predicates
};
2025-02-11 10:42:20 +08:00
fetchResult =
await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC,
fetchOptions);
console.log('get getUserAlbumItemByDisplayName, count: ' + fetchResult.getCount());
2024-05-09 13:42:56 +08:00
if (fetchResult.getCount() > 0) {
album = await fetchResult.getFirstObject();
}
} catch (err) {
2025-02-11 10:42:20 +08:00
console.log('get Album fetchResult failed with err: ' + err);
2024-05-09 13:42:56 +08:00
} finally {
if (fetchResult != null) {
fetchResult.close();
}
}
return album;
}
2025-03-26 10:37:10 +08:00
public async addAssetToAlbum(albumName: string, file_uri: string) {
2025-02-11 10:42:20 +08:00
console.log('baoyihu addAssetToAlbum begin albumName ' + albumName + ', file_uri:' + file_uri);
2024-05-09 13:42:56 +08:00
try {
let album = await this.getUserAlbumItemByDisplayName(albumName);
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.PhotoKeys.URI, file_uri)
2025-03-26 10:37:10 +08:00
let fetchOptions: photoAccessHelper.FetchOptions = {
2024-05-09 13:42:56 +08:00
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
predicates: predicates
};
2025-01-20 08:50:40 +08:00
let assetsResult = await this.userFileMgr.getAssets(fetchOptions);
2024-05-09 13:42:56 +08:00
if (assetsResult.getCount() > 0) {
let asset = await assetsResult.getFirstObject();
2025-02-11 10:42:20 +08:00
console.log('baoyihu addAssetToAlbum one asset uri : ' + asset.uri + ', photoType : ' + asset.photoType +
', displayName : ' + asset.displayName);
2024-05-09 13:42:56 +08:00
await album.addAssets([asset]);
2025-02-11 10:42:20 +08:00
console.log('baoyihu addAssetToAlbum success ');
2024-05-09 13:42:56 +08:00
}
} catch (err) {
2025-02-11 10:42:20 +08:00
console.log('baoyihu addAssetToAlbum failed with err: ' + err);
2024-05-09 13:42:56 +08:00
} finally {
}
}
2025-03-26 10:37:10 +08:00
public async deleteFileOfAlbum(album_Name: string, file_type: number | string | boolean): Promise<void> {
2025-02-11 10:42:20 +08:00
console.log('baoyihu deletePictureOfAlbum album_Name' + album_Name);
2025-03-26 10:37:10 +08:00
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = null;
2024-05-09 13:42:56 +08:00
try {
2025-01-20 08:50:40 +08:00
let album = await this.getUserAlbumItemByName(album_Name);
2024-05-09 13:42:56 +08:00
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.PhotoKeys.PHOTO_TYPE, file_type)
2025-03-26 10:37:10 +08:00
let fetchOptions: photoAccessHelper.FetchOptions = {
2024-05-09 13:42:56 +08:00
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
predicates: predicates
};
photoFetchResult = await album.getAssets(fetchOptions)
2025-03-26 10:37:10 +08:00
let all_fileAsset = await photoFetchResult.getAllObjects();
2025-01-20 08:50:40 +08:00
2025-03-26 10:37:10 +08:00
let uri_array: string[] = []
2025-02-11 10:42:20 +08:00
console.log('baoyihu deletePictureOfAlbum iterator begin', album_Name);
for (let onfile of all_fileAsset) {
console.log(album_Name,
'baoyihu deletePictureOfAlbum uri : ' + onfile.uri + ', photoType : ' + onfile.photoType +
', displayName : ' + onfile.displayName);
2025-01-20 08:50:40 +08:00
uri_array.push(onfile.uri);
}
await this.userFileMgr.deleteAssets(uri_array);
2024-05-09 13:42:56 +08:00
2025-03-26 10:37:10 +08:00
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null;
2024-05-09 13:42:56 +08:00
try {
2025-02-11 10:42:20 +08:00
albumFetchResult =
await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.TRASH);
2024-05-09 13:42:56 +08:00
let trashAlbum = await albumFetchResult.getFirstObject();
trashAlbum.deleteAssets(all_fileAsset).then(() => {
2025-02-11 10:42:20 +08:00
console.log('baoyihu deletePictureOfAlbum trash ok : ', album_Name);
2025-03-26 10:37:10 +08:00
}).catch((err: BusinessError) => {
2025-02-11 10:42:20 +08:00
console.log('baoyihu deletePictureOfAlbum trash faild : ', album_Name);
2024-05-09 13:42:56 +08:00
});
2025-02-11 10:42:20 +08:00
} catch (err) {
console.log('baoyihu deletePictureOfAlbum error: ' + err, album_Name);
2024-05-09 13:42:56 +08:00
} finally {
if (albumFetchResult != null) {
albumFetchResult.close();
}
}
2025-02-11 10:42:20 +08:00
console.log('baoyihu deletePictureOfAlbum delete end', album_Name);
2024-05-09 13:42:56 +08:00
} catch (err) {
2025-02-11 10:42:20 +08:00
console.log('baoyihu deletePictureOfAlbum failed with err: ' + err, album_Name);
2024-05-09 13:42:56 +08:00
} finally {
if (photoFetchResult != null) {
photoFetchResult.close();
}
}
}
2025-01-20 08:50:40 +08:00
public async deleteAllPictures(): Promise<void> {
console.log('baoyihu deleteAllPictures');
2025-03-26 10:37:10 +08:00
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = null;
2024-05-09 13:42:56 +08:00
try {
let predicates = new dataSharePredicates.DataSharePredicates();
2025-03-26 10:37:10 +08:00
let fetchOptions: photoAccessHelper.FetchOptions = {
2024-05-09 13:42:56 +08:00
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
predicates: predicates
};
photoFetchResult = await this.userFileMgr.getAssets(fetchOptions);
2025-03-26 10:37:10 +08:00
let all_fileAsset = await photoFetchResult.getAllObjects();
let uri_array: string[] = []
2025-02-11 10:42:20 +08:00
console.log('baoyihu batch delete begin');
for (let onfile of all_fileAsset) {
2025-01-20 08:50:40 +08:00
uri_array.push(onfile.uri);
2024-05-09 13:42:56 +08:00
}
2025-01-20 08:50:40 +08:00
// await album.removeAssets(all_fileAsset);
2024-05-09 13:42:56 +08:00
await this.userFileMgr.deleteAssets(uri_array);
2025-02-11 10:42:20 +08:00
console.log('baoyihu batch delete end');
2024-05-09 13:42:56 +08:00
} catch (err) {
2025-02-11 10:42:20 +08:00
console.log('baoyihu get Album getPhotoAssets failed with err: ' + err);
2024-05-09 13:42:56 +08:00
} finally {
if (photoFetchResult != null) {
photoFetchResult.close();
}
}
}
2025-02-11 10:42:20 +08:00
2024-05-09 13:42:56 +08:00
//
// async getUserAlbumItemByUri(uri: string): Promise<photoAccessHelper.Album> {
2025-01-20 08:50:40 +08:00
// let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null
// let album: photoAccessHelper.Album = null
// try {
// console.log( 'getUserAlbumItemByUri');
// let predicates = new dataSharePredicates.DataSharePredicates();
// predicates.equalTo(photoAccessHelper.AlbumKeys.URI, uri)
// let fetchOptions = {
// fetchColumns: [],
// predicates: predicates
// };
// fetchResult = await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions );
// console.log( 'get Album fetchResult, count: ' + fetchResult.getCount());
// if (fetchResult.getCount() > 0) {
// album = await fetchResult.getFirstObject();
// }
// } catch (err) {
// console.log( 'get Album fetchResult failed with err: ' + err);
// } finally {
// if (fetchResult != null) {
// fetchResult.close();
// }
// }
// return album;
2024-05-09 13:42:56 +08:00
// }
async getUserAlbumItemByName(albumName: string): Promise<photoAccessHelper.Album> {
2025-01-20 08:50:40 +08:00
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null
2024-05-09 13:42:56 +08:00
let album: photoAccessHelper.Album = null
try {
2025-02-11 10:42:20 +08:00
console.log('getUserAlbumItemByName');
2024-05-09 13:42:56 +08:00
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.AlbumKeys.ALBUM_NAME, albumName)
2025-03-26 10:37:10 +08:00
let fetchOptions: photoAccessHelper.FetchOptions = {
2024-05-09 13:42:56 +08:00
fetchColumns: [],
predicates: predicates
};
2025-02-11 10:42:20 +08:00
fetchResult =
await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC,
fetchOptions);
console.log('get getUserAlbumItemByName, count: ' + fetchResult.getCount());
2024-05-09 13:42:56 +08:00
if (fetchResult.getCount() > 0) {
album = await fetchResult.getFirstObject();
2025-02-11 10:42:20 +08:00
console.log('getUserAlbumItemByName uri: ' + album.albumUri + ',albumName: ' + album.albumName);
2024-05-09 13:42:56 +08:00
}
} catch (err) {
2025-02-11 10:42:20 +08:00
console.log('getUserAlbumItemByName failed with err: ' + err);
2024-05-09 13:42:56 +08:00
} finally {
if (fetchResult != null) {
fetchResult.close();
}
}
return album;
}
2025-03-26 10:37:10 +08:00
public async createAlbum(albumName: string): Promise<string> {
2024-05-09 13:42:56 +08:00
try {
2025-02-11 10:42:20 +08:00
console.log("baoyihu createAlbum beging");
2025-01-20 08:50:40 +08:00
let albumAsset = await this.userFileMgr.createAlbum(albumName);
2025-02-11 10:42:20 +08:00
console.log('baoyihu createAlbum success, albumType: ' + albumAsset.albumType + ', albumSubtype: ' +
albumAsset.albumSubtype
+ ', albumName: ' + albumAsset.albumName + ', albumUri: ' + albumAsset.albumUri + ', coverUri: ' +
albumAsset.coverUri);
return albumAsset.albumUri;
2024-05-09 13:42:56 +08:00
} catch (err) {
2025-02-11 10:42:20 +08:00
console.log('baoyihu createAlbum failed with err: ' + err);
2024-05-09 13:42:56 +08:00
} finally {
}
}
2025-02-11 10:42:20 +08:00
public async createPhotoAsset(type: photoAccessHelper.AlbumType,
subType: photoAccessHelper.AlbumSubtype): Promise<void> {
console.log('baoyihu createPhotoAsset enter ');
2024-05-09 13:42:56 +08:00
try {
2025-03-26 10:37:10 +08:00
let createOptions: photoAccessHelper.PhotoCreateOptions = { subtype: photoAccessHelper.PhotoSubtype.SCREENSHOT };
2025-02-11 10:42:20 +08:00
await this.userFileMgr.createAsset("picture2.jpg", createOptions, (err, photoResult) => {
console.log('baoyihu createPhotoAsset return uri: ' + photoResult.uri + ', photoType: ' + photoResult.photoType
+ ', displayName: ' + photoResult.displayName);
2024-05-09 13:42:56 +08:00
});
} catch (err) {
2025-02-11 10:42:20 +08:00
console.log('baoyihu createPhotoAsset failed with err: ' + err);
2024-05-09 13:42:56 +08:00
} finally {
}
}
2025-02-11 10:42:20 +08:00
public async queryAlbum(type: photoAccessHelper.AlbumType, subType: photoAccessHelper.AlbumSubtype): Promise<string> {
2024-05-09 13:42:56 +08:00
2025-01-20 08:50:40 +08:00
let firstUri = "";
2025-02-11 10:42:20 +08:00
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null;
2024-05-09 13:42:56 +08:00
try {
fetchResult = await this.userFileMgr.getAlbums(type, subType);
2025-02-11 10:42:20 +08:00
console.log('baoyihu queryAlbum success count: ' + fetchResult.getCount());
for (let i = 0; i < fetchResult.getCount(); i++) {
let albumAsset: photoAccessHelper.Album = await fetchResult.getObjectByPosition(i);
console.log('queryAlbum albumType: ' + albumAsset.albumType + ', Subtype: ' + albumAsset.albumSubtype
+ ', Name: ' + albumAsset.albumName + ', Uri: ' + albumAsset.albumUri + ', coverUri: ' + albumAsset.coverUri);
2024-05-09 13:42:56 +08:00
let predicates = new dataSharePredicates.DataSharePredicates();
2025-03-26 10:37:10 +08:00
let fetchOptions: photoAccessHelper.FetchOptions = {
2024-05-09 13:42:56 +08:00
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
predicates: predicates
};
let photoFetchResult = await albumAsset.getAssets(fetchOptions);
2025-01-20 08:50:40 +08:00
let count = photoFetchResult.getCount();
2025-02-11 10:42:20 +08:00
console.log('baoyihu queryAlbum photoFetchResult count: ' + count);
2025-03-26 10:37:10 +08:00
let all_fileAsset = await photoFetchResult.getAllObjects();
2025-01-20 08:50:40 +08:00
// var uri_array=[]
2024-05-09 13:42:56 +08:00
2025-02-11 10:42:20 +08:00
for (let onfile of all_fileAsset) {
console.log('baoyihu queryAlbum one uri : ' + onfile.uri);
2024-05-09 13:42:56 +08:00
firstUri = onfile.uri;
2025-01-20 08:50:40 +08:00
// uri_array.push(onfile.uri);
2024-05-09 13:42:56 +08:00
}
}
2025-02-11 10:42:20 +08:00
console.log('baoyihu queryAlbum return asser:' + firstUri);
2024-05-09 13:42:56 +08:00
} catch (err) {
2025-02-11 10:42:20 +08:00
console.log('baoyihu queryAlbum failed with err: ' + err);
2024-05-09 13:42:56 +08:00
} finally {
if (fetchResult != null) {
fetchResult.close();
}
}
return new Promise((resolve, reject) => {
2025-02-11 10:42:20 +08:00
console.log('baoyihu queryAlbum before resolve:' + firstUri);
2024-05-09 13:42:56 +08:00
resolve(firstUri)
})
}
2025-02-11 10:42:20 +08:00
public async deleteAllVideos(type: photoAccessHelper.AlbumType,
subType: photoAccessHelper.AlbumSubtype): Promise<void> {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null;
2024-05-09 13:42:56 +08:00
try {
fetchResult = await this.userFileMgr.getAlbums(type, subType);
2025-02-11 10:42:20 +08:00
console.log('baoyihu_ get Album fetchResult, count: ' + fetchResult.getCount());
for (let i = 0; i < fetchResult.getCount(); i++) {
let albumAsset: photoAccessHelper.Album = await fetchResult.getObjectByPosition(i);
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = null;
2024-05-09 13:42:56 +08:00
let count = 0;
2025-02-11 10:42:20 +08:00
console.log('baoyihu_ get one Album name : ' + albumAsset.albumName);
2024-05-09 13:42:56 +08:00
try {
let predicates = new dataSharePredicates.DataSharePredicates();
2025-03-26 10:37:10 +08:00
let fetchOptions: photoAccessHelper.FetchOptions = {
2024-05-09 13:42:56 +08:00
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
predicates: predicates
};
photoFetchResult = await albumAsset.getAssets(fetchOptions);
count = photoFetchResult.getCount();
2025-02-11 10:42:20 +08:00
console.log('baoyihu_ photoFetchResult count: ' + count);
2025-03-26 10:37:10 +08:00
let all_fileAsset = await photoFetchResult.getAllObjects();
2024-05-09 13:42:56 +08:00
/*
2025-01-20 08:50:40 +08:00
var uri_array=[]
console.log( 'baoyihu batch delete begin' );
for (let onfile of all_fileAsset)
{
console.log( 'baoyihu push one uri : ' + onfile.uri);
uri_array.push(onfile.uri);
}
* */
2024-05-09 13:42:56 +08:00
await albumAsset.removeAssets(all_fileAsset);
//await this.userFileMgr.deleteAssets(uri_array);
2025-02-11 10:42:20 +08:00
console.log('baoyihu_ batch delete end');
2024-05-09 13:42:56 +08:00
} catch (err) {
2025-02-11 10:42:20 +08:00
console.log('baoyihu_ get Album getPhotoAssets failed with err: ' + err);
2024-05-09 13:42:56 +08:00
} finally {
if (photoFetchResult != null) {
photoFetchResult.close();
}
}
}
} catch (err) {
2025-02-11 10:42:20 +08:00
console.log('baoyihu_ get Album fetchResult failed with err: ' + err);
2024-05-09 13:42:56 +08:00
} finally {
if (fetchResult != null) {
fetchResult.close();
}
}
}
2025-01-20 08:50:40 +08:00
}