ImageKnife/entry/src/main/ets/MainAbility/pages/basicTestFileIOPage.ets

137 lines
5.2 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 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/imageknife'
import resourceManager from '@ohos.resourceManager';
@Entry
@Component
struct BasicTestFileIOPage {
@Watch('watchPathChange') @State filePath: string= '查看featureAbility路径';
appFilePath = '';
appCachePath = '';
@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(() => {
featureAbility.getContext()
.getFilesDir()
.then((data) => {
console.log('ImageKnife filesPath = ' + data)
this.filePath = data
this.appFilePath = data;
})
.catch((error) => {
console.error('ImageKnife Failed to obtain the filesPath directory. Cause:' + error.message);
})
})
Button('featureAbility.getContext().getCacheDir()')
.margin({ top: 10 })
.onClick(() => {
featureAbility.getContext()
.getCacheDir()
.then((data) => {
console.log('ImageKnife cachesPath = ' + data)
this.filePath = data
this.appCachePath = data
})
.catch((error) => {
console.error('ImageKnife Failed to obtain the cachesPath directory. Cause:' + error.message);
})
})
Button('files目录创建Folder1和Folder2 验证statSync mkdirSync')
.margin({ top: 10 })
.onClick(() => {
FileUtils.getInstance()
.createFolder(this.appFilePath + '/Folder1');
FileUtils.getInstance()
.createFolder(this.appFilePath + '/Folder2');
})
Button('将media资源存入Folder1 验证writeSync mkdirSync createStreamSync')
.margin({ top: 10 })
.onClick(() => {
resourceManager.getResourceManager()
.then(result => {
result.getMedia($r('app.media.gifSample').id,)
.then(data => {
console.log('basicTestFileIOPage - 本地加载资源 解析后数据data = ' + data)
let arrayBuffer = this.typedArrayToBuffer(data);
FileUtils.getInstance().writeFile(this.appFilePath + '/Folder1/jpgSample.gif', arrayBuffer)
this.imageFile = 'file://' + this.appFilePath + '/Folder1/jpgSample.gif'
})
.catch(err => {
console.log('basicTestFileIOPage - 本地加载资源err' + JSON.stringify(err));
})
})
})
Button('copy:Folder1至Folder2 验证copyFileSync')
.margin({ top: 10 })
.onClick(() => {
let filePath1 = this.appFilePath + '/Folder1/jpgSample.gif';
let filePath2 = this.appFilePath + '/Folder2/jpgSample.gif';
FileUtils.getInstance().createFolder(this.appFilePath + '/Folder2')
FileUtils.getInstance().copyFile(filePath1, filePath2);
this.imageFile = 'file://' + this.appFilePath + '/Folder2/jpgSample.gif'
})
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)
}
}