1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import UserFileManager from '@ohos.filemanagement.userFileManager'; 17import dataSharePredicates from '@ohos.data.dataSharePredicates'; 18import image from '@ohos.multimedia.image'; 19import { Log } from '../utils/Log'; 20import { GlobalContext } from '../utils/GlobalContext'; 21 22const TAG = '[ThumbnailGetter]:'; 23 24export default class ThumbnailGetter { 25 private mUserFileManager: UserFileManager.UserFileManager; 26 private mCameraAlbum: UserFileManager.Album; 27 private mRecentFileUri: string = ''; 28 29 public async getThumbnailInfo(width: number, height: number, uri?: string): Promise<PixelMap | undefined> { 30 this.mUserFileManager = UserFileManager.getUserFileMgr(GlobalContext.get().getCameraAbilityContext()); 31 Log.info(`${TAG} getThumbnailInfo E width: ${width}, height: ${height}, uri: ${uri}`); 32 Log.info(`${TAG} getThumbnailInfo E`); 33 const fileAsset: UserFileManager.FileAsset = await this.getLastFileAsset(); 34 if (!fileAsset) { 35 Log.info(`${TAG} getThumbnailInfo getLastFileAsset error: fileAsset undefined.`); 36 return undefined; 37 } 38 let thumbnailPixelMap: image.PixelMap = <image.PixelMap> await fileAsset.getThumbnail({ 39 width: width, height: height 40 // @ts-ignore 41 }).catch(e => { 42 Log.error(`${TAG} getThumbnail error: ${JSON.stringify(e)}`); 43 }); 44 if (thumbnailPixelMap === undefined) { 45 Log.info(`${TAG} getThumbnail successful ` + thumbnailPixelMap); 46 } else { 47 Log.info(`${TAG} getThumbnail fail`); 48 } 49 Log.info(`${TAG} getThumbnailInfo X`); 50 return thumbnailPixelMap; 51 } 52 53 public async getLastFileAsset(): Promise<UserFileManager.FileAsset> { 54 let predicates = new dataSharePredicates.DataSharePredicates(); 55 predicates.orderByDesc('date_added').limit(1, 0); 56 let fetchOptions = { 57 fetchColumns: ['date_added'], 58 predicates: predicates, 59 }; 60 Log.info(`${TAG} getLastFileAsset fetchOp: ${JSON.stringify(fetchOptions)}`); 61 return this.getFileAssetByFetchOp(fetchOptions); 62 } 63 64 public getRecentFileUri(): string { 65 return this.mRecentFileUri; 66 } 67 68 public async getFileAssetByFetchOp(fetchOp): Promise<UserFileManager.FileAsset> { 69 let fetchResult; 70 let fileAsset; 71 try { 72 await this.createCameraAlbum(); 73 fetchResult = await this.mCameraAlbum?.getPhotoAssets(fetchOp); 74 if (fetchResult !== undefined) { 75 Log.info(`${TAG} getFileAssetByFetchOp fetchResult success`); 76 fileAsset = await fetchResult.getLastObject(); 77 if (fileAsset !== undefined) { 78 Log.info(`${TAG} getFileAssetByFetchOp fileAsset.displayName : ${JSON.stringify(fileAsset.displayName)}`); 79 } 80 } 81 } catch (e) { 82 Log.error(`${TAG} getFileAssetByFetchOp get fileAsset error: ${JSON.stringify(e)}`); 83 } finally { 84 fetchResult.close(); 85 } 86 this.mRecentFileUri = fileAsset?.uri; 87 Log.info(`${TAG} mRecentFileUri : ${JSON.stringify(this.mRecentFileUri)}`); 88 return fileAsset; 89 } 90 91 private async createCameraAlbum(): Promise<void> { 92 Log.log(`${TAG} createCameraAlbum E`); 93 if (!this.mCameraAlbum) { 94 let fetchResult = 95 await this.mUserFileManager?.getAlbums(UserFileManager.AlbumType.SYSTEM, UserFileManager.AlbumSubType.CAMERA); 96 this.mCameraAlbum = await fetchResult?.getFirstObject(); 97 Log.log(`${TAG} createCameraAlbum albumUri: ${JSON.stringify(this.mCameraAlbum.albumUri)}`); 98 } 99 Log.log(`${TAG} createCameraAlbum X`); 100 } 101}