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 mediaLibrary from '@ohos.multimedia.mediaLibrary' 17import Logger from '../util/Logger' 18 19const TAG: string = 'MediaUtils' 20 21class MediaUtils { 22 async getFileAssetsFromType(mediaType: number, context: any) { 23 let mediaList: Array<mediaLibrary.FileAsset> = [] 24 let mediaLib: mediaLibrary.MediaLibrary = mediaLibrary.getMediaLibrary(context) 25 Logger.info(TAG, `getFileAssetsFromType,mediaType: ${mediaType}`) 26 let fileKeyObj = mediaLibrary.FileKey 27 28 let fetchOption = { 29 selections: `${fileKeyObj.MEDIA_TYPE}=?`, 30 selectionArgs: [`${mediaType}`] 31 } 32 33 // 获取文件资源 34 let fetchFileResult = await mediaLib.getFileAssets(fetchOption) 35 Logger.info(TAG, `getFileAssetsFromType,fetchFileResult.count: ${fetchFileResult.getCount()}`) 36 // getCount 获取文件检索结果中的文件总数。 37 if (fetchFileResult.getCount() > 0) { 38 mediaList = await fetchFileResult.getAllObject() // 获取文件检索结果中的所有文件资产。此方法返回FileAsset结果集。 39 } 40 return mediaList 41 } 42} 43 44export default new MediaUtils()