forked from floraachy/ImageKnife
162 lines
6.7 KiB
Plaintext
162 lines
6.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 { FileUtils, ImageKnifeGlobal} from '@ohos/libraryimageknife'
|
||
import resourceManager from '@ohos.resourceManager';
|
||
import { BusinessError } from '@ohos.base'
|
||
import common from '@ohos.app.ability.common';
|
||
@Entry
|
||
@Component
|
||
struct basicTestFileIOPage {
|
||
@State filePath: string = '查看featureAbility路径';
|
||
appFilePath = '';
|
||
appCachePath = '';
|
||
@State imageHint: string = '文字提醒1'
|
||
@State imageHint2: string = '文字提醒2'
|
||
@State imageFile: string = ''
|
||
|
||
@State imageRes: Resource = $r('app.media.pngSample')
|
||
@State imagePixelMap?: PixelMap = undefined
|
||
@State normalPixelMap: boolean = false;
|
||
@State normalResource: boolean = false;
|
||
|
||
watchPathChange() {
|
||
console.log('watchPathChange');
|
||
}
|
||
|
||
build() {
|
||
Scroll() {
|
||
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
|
||
Text(this.filePath).fontSize(20)
|
||
Button('featureAbility.getContext().getFilesDir()')
|
||
.margin({ top: 10 })
|
||
.onClick(() => {
|
||
|
||
let data:string = (ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext).filesDir as string;
|
||
console.log('ImageKnife filesPath = ' + data)
|
||
this.filePath = data
|
||
this.appFilePath = data;
|
||
|
||
})
|
||
Button('featureAbility.getContext().getCacheDir()')
|
||
.margin({ top: 10 })
|
||
.onClick(() => {
|
||
|
||
let data:string = (ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext).cacheDir as string;
|
||
console.log('ImageKnife cachesPath = ' + data)
|
||
this.filePath = data
|
||
this.appFilePath = data;
|
||
|
||
})
|
||
Text(this.imageHint)
|
||
Button('files目录创建Folder1和Folder2 验证statSync mkdirSync')
|
||
.margin({ top: 10 })
|
||
.onClick(() => {
|
||
if(this.appFilePath == '' || this.appFilePath == null){
|
||
this.imageHint = 'appFilePath未取到值,请按顺序从上往下,从左往右依次测试'
|
||
return
|
||
}
|
||
console.log('files目录创建Folder1和Folder2 验证statSync mkdirSync')
|
||
try {
|
||
FileUtils.getInstance()
|
||
.createFolder(this.appFilePath + '/Folder1');
|
||
FileUtils.getInstance()
|
||
.createFolder(this.appFilePath + '/Folder2');
|
||
} catch (e) {
|
||
console.log('appFilePath未取到值,请按顺序从上往下,从左往右依次测试"'+JSON.stringify(e));
|
||
}
|
||
})
|
||
Button('将media资源存入Folder1 验证writeSync mkdirSync createStreamSync')
|
||
.margin({ top: 10 })
|
||
.onClick(() => {
|
||
console.log('将media资源存入Folder1 验证writeSync mkdirSync createStreamSync')
|
||
if(this.appFilePath == '' || this.appFilePath == null){
|
||
this.appFilePath = 'appFilePath未取到值,请按顺序从上往下,从左往右依次测试'
|
||
return
|
||
}
|
||
((ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext).resourceManager as resourceManager.ResourceManager)
|
||
.getMediaContent($r('app.media.gifSample').id)
|
||
.then(data => {
|
||
console.log('result.getMedia')
|
||
console.log('basicTestFileIOPage - 本地加载资源 解析后数据data length= ' + data.byteLength)
|
||
let arrayBuffer = this.typedArrayToBuffer(data);
|
||
FileUtils.getInstance().writeFile(this.appFilePath + '/Folder1/jpgSample.gif', arrayBuffer)
|
||
this.imageFile = 'file://' + this.appFilePath + '/Folder1/jpgSample.gif'
|
||
console.log('Folder1 imaeFile =' + this.imageFile)
|
||
})
|
||
.catch((err:BusinessError) => {
|
||
console.log('basicTestFileIOPage - 本地加载资源err' + JSON.stringify(err as BusinessError));
|
||
})
|
||
})
|
||
Text(this.imageHint2)
|
||
Button('copy:Folder1至Folder2, 验证copyFileSync')
|
||
.margin({ top: 10 })
|
||
.onClick(() => {
|
||
console.log('copy:Folder1至Folder2, 验证copyFileSync')
|
||
if(this.appFilePath == '' || this.appFilePath == null){
|
||
this.imageHint2 = 'appFilePath未取到值,请按顺序从上往下,从左往右依次测试'
|
||
return
|
||
}
|
||
try {
|
||
let filePath1 = this.appFilePath + '/Folder1/jpgSample.gif';
|
||
let filePath2 = this.appFilePath + '/Folder2/jpgSample.gif';
|
||
FileUtils.getInstance().createFolder(this.appFilePath + '/Folder1')
|
||
FileUtils.getInstance().createFolder(this.appFilePath + '/Folder2')
|
||
FileUtils.getInstance().copyFile(filePath1, filePath2);
|
||
this.imageFile = 'file://' + this.appFilePath + '/Folder2/jpgSample.gif'
|
||
console.log('Folder2 imaeFile =' + this.imageFile)
|
||
} catch (e) {
|
||
console.log('appFilePath未取到值,请按顺序从上往下,从左往右依次测试:'+JSON.stringify(e))
|
||
}
|
||
|
||
})
|
||
Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
|
||
Button('显示空PixelMap')
|
||
.margin({ left: 10 })
|
||
.onClick(() => {
|
||
this.normalPixelMap = true;
|
||
this.normalResource = false;
|
||
})
|
||
Button('显示RES')
|
||
.margin({ left: 10 })
|
||
.onClick(() => {
|
||
this.normalPixelMap = false;
|
||
this.normalResource = true;
|
||
})
|
||
Button('显示String')
|
||
.margin({ left: 10 })
|
||
.onClick(() => {
|
||
this.normalPixelMap = false;
|
||
this.normalResource = false;
|
||
})
|
||
}
|
||
|
||
|
||
Image(this.normalPixelMap ? this.imagePixelMap : (this.normalResource ? this.imageRes : this.imageFile))
|
||
.width(200)
|
||
.height(200)
|
||
.backgroundColor(Color.Pink)
|
||
}
|
||
.width('100%')
|
||
.height('100%')
|
||
}
|
||
|
||
}
|
||
|
||
typedArrayToBuffer(array: Uint8Array): ArrayBuffer {
|
||
return array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset)
|
||
}
|
||
}
|