• 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 mediaLibrary from '@ohos.multimedia.mediaLibrary'
17
18import { Log } from '../utils/Log'
19
20export default class ThumbnailGetter {
21  private TAG = '[ThumbnailGetter]:'
22
23  public async getThumbnailInfo(width: number, height: number, uri?: string): Promise<PixelMap | undefined> {
24    Log.log(`${this.TAG} getThumbnailInfo E`)
25    Log.debug(`${this.TAG} getThumbnailInfo width: ${width}, height: ${height}, uri: ${JSON.stringify(uri)}`)
26    const fileKeyObj = mediaLibrary.FileKey;
27    let fetchOp: any
28    const media = mediaLibrary.getMediaLibrary(globalThis.cameraAbilityContext);
29    let publicPath: string = await media.getPublicDirectory(mediaLibrary.DirectoryType.DIR_CAMERA)
30    Log.log(`${this.TAG} getThumbnailInfo media: ${media}`)
31    fetchOp = {
32      selections: `${fileKeyObj.RELATIVE_PATH}=?`,
33      selectionArgs: [publicPath],
34      order: `${fileKeyObj.DATE_ADDED} DESC LIMIT 0, 1`
35    }
36
37    Log.log(`${this.TAG} getThumbnailInfo fetchOp: ${JSON.stringify(fetchOp)}`)
38    const fetchFileResult = await media.getFileAssets(fetchOp);
39    const count = fetchFileResult.getCount()
40    Log.log(`${this.TAG} getThumbnailInfo fetchFileResult.getCount: ${count}`)
41    if (count == 0) {
42      return undefined
43    }
44    const lastFileAsset = await fetchFileResult.getLastObject()
45    await fetchFileResult.close()
46    if (lastFileAsset == null) {
47      Log.error(`${this.TAG} getThumbnailInfo lastFileAsset is null`)
48      return undefined
49    }
50    const thumbnailPixelMap = lastFileAsset.getThumbnail({width: 40, height: 40})
51    Log.info(`${this.TAG} getThumbnailInfo thumbnailPixelMap: ${JSON.stringify(thumbnailPixelMap)} X`)
52    return thumbnailPixelMap
53  }
54}
55