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 router from '@ohos.router' 18import Logger from '../model/Logger' 19import MediaUtils from '../model/MediaUtils' 20import MediaDataSource from '../common/BasicDataSource' 21import TitleBar from '../common/TitleBar' 22import VideoItem from '../common/VideoItem' 23 24const TAG: string = 'Index' 25 26@Entry 27@Component 28struct Index { 29 private scroller: Scroller = new Scroller() 30 private mediaDataSource:MediaDataSource = new MediaDataSource([]) 31 private mediaUtil: MediaUtils = new MediaUtils(getContext(this) as any) 32 33 async aboutToAppear() { 34 router.clear() 35 Logger.info(TAG, 'aboutToAppear') 36 let context = getContext(this) as any 37 await context.requestPermissionsFromUser(['ohos.permission.READ_MEDIA']) 38 await this.getData() 39 } 40 41 async getData() { 42 this.mediaDataSource['dataArray'] = [] 43 let fileList = await this.mediaUtil.getFileAssetsFromType(mediaLibrary.MediaType.VIDEO) 44 this.mediaDataSource['dataArray'] = fileList 45 this.mediaDataSource.notifyDataReload() 46 } 47 48 build() { 49 Column() { 50 TitleBar() 51 Text($r('app.string.local_videos')) 52 .width('100%') 53 .fontSize(30) 54 .textAlign(TextAlign.Start) 55 .padding(10) 56 Grid(this.scroller) { 57 LazyForEach(this.mediaDataSource, item => { 58 GridItem() { 59 VideoItem({ media: item }) 60 } 61 }, item => item.title) 62 } 63 .columnsTemplate('1fr 1fr') 64 .columnsGap(10) 65 .rowsGap(10) 66 .layoutWeight(1) 67 .backgroundColor('#F5F5F5') 68 .padding(10) 69 } 70 .height('100%') 71 } 72}