87 lines
3.7 KiB
Plaintext
87 lines
3.7 KiB
Plaintext
/*
|
|
* Copyright (C) 2021 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 featureAbility from '@ohos.ability.featureAbility';
|
|
import {FileUtils} from '@ohos/libraryimageknife'
|
|
import {FileTypeUtil} from '@ohos/libraryimageknife'
|
|
import resourceManager from '@ohos.resourceManager';
|
|
import {Base64} from '@ohos/libraryimageknife'
|
|
import {ImageKnifeGlobal} from '@ohos/libraryimageknife'
|
|
import { BusinessError } from '@ohos.base'
|
|
import common from '@ohos.app.ability.common';
|
|
@Entry
|
|
@Component
|
|
struct BasicTestResourceManagerPage {
|
|
@State fileTypeStr: string = '解析后图片类型';
|
|
|
|
build() {
|
|
Scroll() {
|
|
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
|
|
Text(this.fileTypeStr).fontSize(20)
|
|
.width(300).height(200).backgroundColor(Color.Pink)
|
|
Button('getMedia解析一张jpg图片')
|
|
.margin({ top: 10 })
|
|
.onClick(() => {
|
|
((ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext).resourceManager as resourceManager.ResourceManager)
|
|
.getMediaContent($r('app.media.jpgSample')
|
|
.id)
|
|
.then(data => {
|
|
console.log('basicTestFileIOPage - 本地加载资源 解析后数据data = ' + data)
|
|
let arrayBuffer = this.typedArrayToBuffer(data);
|
|
let filetypeUtil = new FileTypeUtil();
|
|
let fileType = filetypeUtil.getFileType(arrayBuffer);
|
|
if(fileType != null) {
|
|
this.fileTypeStr = fileType;
|
|
}
|
|
})
|
|
.catch((err:BusinessError) => {
|
|
console.log('basicTestFileIOPage - 本地加载资源err' + JSON.stringify(err));
|
|
})
|
|
})
|
|
Button('getMediaBase64解析一张png图片')
|
|
.margin({ top: 10 })
|
|
.onClick(() => {
|
|
((ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext).resourceManager as resourceManager.ResourceManager)
|
|
.getMediaContentBase64($r('app.media.pngSample')
|
|
.id)
|
|
.then(data => {
|
|
console.log('ParseResClientBase64 - 本地加载资源 解析后数据data')
|
|
let matchReg = ';base64,';
|
|
let firstIndex = data.indexOf(matchReg);
|
|
data = data.substring(firstIndex + matchReg.length, data.length)
|
|
console.log('ParseResClientBase64 - 本地加载资源 解析后数据剔除非必要数据后data= ' + data)
|
|
let arrayBuffer = Base64.getInstance()
|
|
.decode(data);
|
|
let filetypeUtil = new FileTypeUtil();
|
|
let fileType = filetypeUtil.getFileType(arrayBuffer);
|
|
if(fileType != null) {
|
|
this.fileTypeStr = fileType;
|
|
}
|
|
})
|
|
.catch((err:BusinessError) => {
|
|
console.log('basicTestFileIOPage - 本地加载资源err' + JSON.stringify(err));
|
|
})
|
|
})
|
|
|
|
}
|
|
}
|
|
.width('100%')
|
|
.height('100%')
|
|
}
|
|
|
|
typedArrayToBuffer(array: Uint8Array): ArrayBuffer {
|
|
return array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset)
|
|
}
|
|
}
|