代码拉取完成,页面将自动刷新
同步操作将从 OpenHarmony/applications_app_samples 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
/*
* Copyright (c) 2022-2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import image from '@ohos.multimedia.image'
import mediaLibrary from '@ohos.multimedia.mediaLibrary'
import Logger from '../../utils/Logger'
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import type { Permissions } from '@ohos.abilityAccessCtrl';
/**
* 主要封装了mediaLibrary库相关的接口
*/
class MediaLibraryManager {
requestPermission(context): void {
let permissions: Array<Permissions> = [
'ohos.permission.READ_MEDIA',
'ohos.permission.WRITE_MEDIA'
]
let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
atManager.requestPermissionsFromUser(context, permissions, (code, result) => {
Logger.debug('permissionRequest ' + JSON.stringify(code) + ' Result: ' + JSON.stringify(result))
})
}
async getPixelMapByFileAsset(fileAsset: mediaLibrary.FileAsset): Promise<image.PixelMap> {
if (fileAsset == undefined) {
Logger.error('fileAsset undefined')
// 异常情况下统一返回undefined,不建议使用null
return undefined
}
Logger.debug('begin getPixelMapByFileAsset:' + fileAsset.displayName)
let fd: number = undefined
let pixelMap = undefined
try {
fd = await fileAsset.open('rw')
Logger.debug('getPixelMapByFileAsset fd: ' + fd)
let imageSource = image.createImageSource(fd)
Logger.debug('imageSource: ' + JSON.stringify(imageSource))
let decodingOptions = {
sampleSize: 1,
editable: true,
desiredSize: { width: 3000, height: 4000 },
rotate: 0,
desiredPixelFormat: 3,
desiredRegion: { size: { height: 6000, width: 8000 }, x: 0, y: 0 },
index: 0
}
pixelMap = await imageSource.createPixelMap(decodingOptions)
Logger.debug('pixel size: ' + pixelMap.getPixelBytesNumber())
fileAsset.close(fd)
} catch (err) {
Logger.debug('err: ' + JSON.stringify(err))
}
return pixelMap
}
getMediaLibrary(context): mediaLibrary.MediaLibrary {
return mediaLibrary.getMediaLibrary(context)
}
async getFileAssets(context, fileType: mediaLibrary.MediaType): Promise<mediaLibrary.FetchFileResult> {
Logger.debug('begin getFileAssets, fileType:' + fileType)
let fileKeyObj = mediaLibrary.FileKey
let imagesFetchOption = {
selections: fileKeyObj.MEDIA_TYPE + '= ?',
selectionArgs: [fileType.toString()],
}
let fetchFileResult: mediaLibrary.FetchFileResult = undefined
try {
fetchFileResult = await this.getMediaLibrary(context).getFileAssets(imagesFetchOption)
Logger.debug('fetchFileResult count:' + fetchFileResult.getCount())
} catch (error) {
Logger.error('fetchFileResult Error: ' + JSON.stringify(error))
}
return fetchFileResult
}
async getFileAssetsByName(context, name: string): Promise<mediaLibrary.FileAsset> {
Logger.debug('begin getFileAssetsByName: ' + name)
let fileKeyObj = mediaLibrary.FileKey
let imagesFetchOption = {
selections: fileKeyObj.DISPLAY_NAME + '= ?',
selectionArgs: [name.toString()],
}
let fetchFileResult: mediaLibrary.FetchFileResult = undefined
let file: mediaLibrary.FileAsset = undefined
try {
fetchFileResult = await this.getMediaLibrary(context).getFileAssets(imagesFetchOption)
Logger.debug('fetchFileResult count:' + fetchFileResult.getCount())
file = await fetchFileResult.getFirstObject()
} catch (error) {
Logger.error('fetchFileResult Error: ' + JSON.stringify(error))
}
return file
}
async getThumbnail(fileAsset: mediaLibrary.FileAsset): Promise<image.PixelMap> {
let thumbnail = undefined
try {
thumbnail = await fileAsset.getThumbnail()
Logger.debug('PixelMap size: ' + thumbnail.getPixelBytesNumber())
} catch (error) {
Logger.error('getThumbnail Error: ' + JSON.stringify(error))
}
return thumbnail
}
async createFileAsset(context, mediaType: mediaLibrary.MediaType,
dir: mediaLibrary.DirectoryType, fileName: string): Promise<mediaLibrary.FileAsset> {
Logger.debug('createFileAsset: ' + fileName)
let media = this.getMediaLibrary(context)
let path = await media.getPublicDirectory(dir)
return await media.createAsset(mediaType, fileName, path)
}
async deleteFileAsset(fileAsset: mediaLibrary.FileAsset): Promise<void> {
Logger.debug('deleteFileAsset:' + fileAsset.displayName);
await fileAsset.trash(true);
}
}
export default new MediaLibraryManager()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。