• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
26  private mUserFileManager: UserFileManager.UserFileManager;
27  private mCameraAlbum: UserFileManager.Album;
28  private mRecentFileUri: string = '';
29
30  public async getThumbnailInfo(width: number, height: number, uri?: string): Promise<PixelMap | undefined> {
31    this.mUserFileManager = UserFileManager.getUserFileMgr(GlobalContext.get().getCameraAbilityContext());
32    Log.info(`${TAG} getThumbnailInfo E width: ${width}, height: ${height}, uri: ${uri}`);
33    Log.info(`${TAG} getThumbnailInfo E`);
34    const fileAsset: UserFileManager.FileAsset = await this.getLastFileAsset();
35    if (!fileAsset) {
36      Log.info(`${TAG} getThumbnailInfo getLastFileAsset error: fileAsset undefined.`);
37      return undefined;
38    }
39    let thumbnailPixelMap: image.PixelMap = <image.PixelMap> await fileAsset.getThumbnail({
40      width: width, height: height
41      // @ts-ignore
42    }).catch(e => {
43      Log.error(`${TAG} getThumbnail error: ${JSON.stringify(e)}`);
44    });
45    if (thumbnailPixelMap === undefined) {
46      Log.info(`${TAG} getThumbnail successful ` + thumbnailPixelMap);
47    } else {
48      Log.info(`${TAG} getThumbnail fail`);
49    }
50    Log.info(`${TAG} getThumbnailInfo X`);
51    return thumbnailPixelMap;
52  }
53
54  public async getLastFileAsset(): Promise<UserFileManager.FileAsset> {
55    let predicates = new dataSharePredicates.DataSharePredicates();
56    predicates.orderByDesc('date_added').limit(1, 0);
57    let fetchOptions = {
58      fetchColumns: ['date_added'],
59      predicates: predicates,
60    };
61    Log.info(`${TAG} getLastFileAsset fetchOp: ${JSON.stringify(fetchOptions)}`);
62    return this.getFileAssetByFetchOp(fetchOptions);
63  }
64
65  private async createCameraAlbum(): Promise<void> {
66    Log.log(`${TAG} createCameraAlbum E`);
67    if (!this.mCameraAlbum) {
68      let fetchResult = await this.mUserFileManager?.getAlbums(UserFileManager.AlbumType.SYSTEM, UserFileManager.AlbumSubType.CAMERA);
69      this.mCameraAlbum = await fetchResult?.getFirstObject();
70      Log.log(`${TAG} createCameraAlbum albumUri: ${JSON.stringify(this.mCameraAlbum.albumUri)}`);
71    }
72    Log.log(`${TAG} createCameraAlbum X`);
73  }
74
75  public getRecentFileUri(): string {
76    return this.mRecentFileUri;
77  }
78
79  public async getFileAssetByFetchOp(fetchOp): Promise<UserFileManager.FileAsset> {
80    let fetchResult;
81    let fileAsset;
82    try {
83      await this.createCameraAlbum();
84      fetchResult = await this.mCameraAlbum?.getPhotoAssets(fetchOp);
85      if (fetchResult !== undefined) {
86        Log.info(`${TAG} getFileAssetByFetchOp fetchResult success`);
87        fileAsset = await fetchResult.getLastObject();
88        if (fileAsset !== undefined) {
89          Log.info(`${TAG} getFileAssetByFetchOp fileAsset.displayName : ${JSON.stringify(fileAsset.displayName)}`);
90        }
91      }
92    } catch (e) {
93      Log.error(`${TAG} getFileAssetByFetchOp get fileAsset error: ${JSON.stringify(e)}`);
94    } finally {
95      fetchResult.close();
96    }
97    this.mRecentFileUri = fileAsset?.uri;
98    Log.info(`${TAG} mRecentFileUri : ${JSON.stringify(this.mRecentFileUri)}`);
99    return fileAsset;
100  }
101}