• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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 photoAccessHelper from '@ohos.file.photoAccessHelper';
17import dataSharePredicates from '@ohos.data.dataSharePredicates';
18import Logger from '../util/Logger'
19
20const TAG: string = 'MediaUtils'
21
22class MediaUtils {
23  async getFileAssetsFromType(mediaType: number, context: any) {
24    let mediaList: Array<photoAccessHelper.PhotoAsset> = [];
25    let mediaLib: photoAccessHelper.PhotoAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
26    Logger.info(TAG, `getFileAssetsFromType,mediaType: ${mediaType}`);
27    // 获取文件资源
28    let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
29    let fetchOptions: photoAccessHelper.FetchOptions = {
30      fetchColumns: [`${mediaType}`],
31      predicates: predicates
32    };
33    try {
34      let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await mediaLib.getAssets(fetchOptions);
35      if (fetchResult !== undefined) {
36        Logger.info(TAG, 'fetchResult success');
37        Logger.info(TAG, `getFileAssetsFromType,fetchFileResult.count: ${fetchResult.getCount()}`)
38        // getCount 获取文件检索结果中的文件总数。
39        if (fetchResult.getCount() > 0) {
40          mediaList = await fetchResult.getAllObjects(); // 获取文件检索结果中的所有文件资产。此方法返回FileAsset结果集。
41        }
42      }
43    } catch (err) {
44      Logger.error(TAG,`getAssets failed, error: ${err.code}, ${err.message}`);
45    }
46    return mediaList
47  }
48}
49
50export default new MediaUtils()