forked from floraachy/ImageKnife
1.新增功能:加载媒体图库的uri图片
2.新增测试页面,dataShareUriLoadPage.ets用于测试加载媒体图库的uri图片 Signed-off-by: zhoulisheng1 <zhoulisheng1@huawei.com>
This commit is contained in:
parent
1b5aea1138
commit
be3c4eb03a
|
@ -3,6 +3,8 @@ import hilog from '@ohos.hilog';
|
||||||
import window from '@ohos.window';
|
import window from '@ohos.window';
|
||||||
import { ImageKnife,ImageKnifeDrawFactory } from '@ohos/imageknife'
|
import { ImageKnife,ImageKnifeDrawFactory } from '@ohos/imageknife'
|
||||||
import { CustomEngineKeyImpl } from './CustomEngineKeyImpl'
|
import { CustomEngineKeyImpl } from './CustomEngineKeyImpl'
|
||||||
|
import abilityAccessCtrl,{Permissions} from '@ohos.abilityAccessCtrl';
|
||||||
|
|
||||||
|
|
||||||
export default class EntryAbility extends UIAbility {
|
export default class EntryAbility extends UIAbility {
|
||||||
onCreate(want, launchParam) {
|
onCreate(want, launchParam) {
|
||||||
|
@ -22,6 +24,18 @@ export default class EntryAbility extends UIAbility {
|
||||||
// Main window is created, set main page for this ability
|
// Main window is created, set main page for this ability
|
||||||
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
|
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
|
||||||
|
|
||||||
|
let list : Array<Permissions> = ['ohos.permission.MEDIA_LOCATION','ohos.permission.READ_MEDIA'];
|
||||||
|
let permissionRequestResult;
|
||||||
|
let atManager = abilityAccessCtrl.createAtManager();
|
||||||
|
atManager.requestPermissionsFromUser(this.context, list, (err,result)=>{
|
||||||
|
if(err){
|
||||||
|
console.log("dodo requestPermissionsFromUserError:"+JSON.stringify(err));
|
||||||
|
}else{
|
||||||
|
permissionRequestResult = result;
|
||||||
|
console.log("dodo permissionRequestResult:"+JSON.stringify(permissionRequestResult))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
windowStage.loadContent('pages/index', (err, data) => {
|
windowStage.loadContent('pages/index', (err, data) => {
|
||||||
if (err.code) {
|
if (err.code) {
|
||||||
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
|
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
|
||||||
|
|
|
@ -0,0 +1,104 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 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 mediaLibrary from '@ohos.multimedia.mediaLibrary';
|
||||||
|
import {
|
||||||
|
ImageKnifeComponent,
|
||||||
|
ImageKnifeOption,
|
||||||
|
} from '@ohos/imageknife'
|
||||||
|
import ArkWorker from '@ohos.worker'
|
||||||
|
@Entry
|
||||||
|
@Component
|
||||||
|
struct DataShareUriLoadPage {
|
||||||
|
private globalGifWorker:any = undefined
|
||||||
|
@State imageKnifeOption1: ImageKnifeOption =
|
||||||
|
{
|
||||||
|
loadSrc: $r('app.media.icon'),
|
||||||
|
|
||||||
|
placeholderSrc: $r('app.media.icon_loading'),
|
||||||
|
errorholderSrc: $r('app.media.icon_failed')
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
build() {
|
||||||
|
Scroll() {
|
||||||
|
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
|
||||||
|
Text("获取媒体图库的uri用ImageKnife展示").fontSize(15)
|
||||||
|
Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
|
||||||
|
Button("点击加载Uri并展示")
|
||||||
|
.onClick(() => {
|
||||||
|
// 获取mediaLibrary实例,后续用到此实例均采用此处获取的实例
|
||||||
|
const context = getContext(this);
|
||||||
|
let media = mediaLibrary.getMediaLibrary(context);
|
||||||
|
|
||||||
|
let fileKeyObj = mediaLibrary.FileKey;
|
||||||
|
let imageType = mediaLibrary.MediaType.IMAGE;
|
||||||
|
// 创建文件获取选项,此处参数为获取image类型的文件资源
|
||||||
|
let imagesFetchOp = {
|
||||||
|
selections: fileKeyObj.MEDIA_TYPE + '= ?',
|
||||||
|
selectionArgs: [imageType.toString()],
|
||||||
|
};
|
||||||
|
// 获取文件资源,使用callback方式返回异步结果
|
||||||
|
media.getFileAssets(imagesFetchOp, (error, fetchFileResult) => {
|
||||||
|
// 判断获取的文件资源的检索结果集是否为undefined,若为undefined则接口调用失败
|
||||||
|
if (fetchFileResult == undefined) {
|
||||||
|
console.log('get fetchFileResult failed with error: ' + error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 获取文件检索结果集中的总数
|
||||||
|
const count = fetchFileResult.getCount();
|
||||||
|
// 判断结果集中的数量是否小于0,小于0时表示接口调用失败
|
||||||
|
if (count < 0) {
|
||||||
|
console.log('get count from fetchFileResult failed, count: ' + count);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 判断结果集中的数量是否等于0,等于0时表示接口调用成功,但是检索结果集为空,请检查文件获取选项参数配置是否有误和设备中是否存在相应文件
|
||||||
|
if (count == 0) {
|
||||||
|
console.log('The count of fetchFileResult is zero');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log('Get fetchFileResult successfully, count: ' + count);
|
||||||
|
// 获取文件检索结果集中的第一个资源,使用callback方式返回异步结果
|
||||||
|
fetchFileResult.getFirstObject((error, fileAsset) => {
|
||||||
|
// 检查获取的第一个资源是否为undefined,若为undefined则接口调用失败
|
||||||
|
if (fileAsset == undefined) {
|
||||||
|
console.log('get first object failed with error: ' + error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log("fileAsset id="+fileAsset.id + " fileAsset uri="+fileAsset.uri + " fileAsset displayName="+fileAsset.displayName)
|
||||||
|
|
||||||
|
// 加载图库第一张图片的uri
|
||||||
|
this.imageKnifeOption1 = {
|
||||||
|
loadSrc: fileAsset.uri,
|
||||||
|
placeholderSrc: $r('app.media.icon_loading'),
|
||||||
|
errorholderSrc: $r('app.media.icon_failed')
|
||||||
|
}
|
||||||
|
// 释放FetchFileResult实例并使其失效。无法调用其他方法
|
||||||
|
fetchFileResult.close();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}).margin({ top: 5, left: 3 })
|
||||||
|
ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption1 }).width(300).height(300)
|
||||||
|
}.width('100%').backgroundColor(Color.Pink)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.width('100%')
|
||||||
|
.height('100%')
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -139,6 +139,11 @@ struct IndexFunctionDemo {
|
||||||
console.log("重试Retry")
|
console.log("重试Retry")
|
||||||
router.push({ uri: "pages/frescoRetryTestCasePage" });
|
router.push({ uri: "pages/frescoRetryTestCasePage" });
|
||||||
}).margin({ top: 5, left: 3 })
|
}).margin({ top: 5, left: 3 })
|
||||||
|
Button("加载媒体库uri")
|
||||||
|
.onClick(() => {
|
||||||
|
console.log("加载媒体库uri")
|
||||||
|
router.push({ uri: "pages/dataShareUriLoadPage" });
|
||||||
|
}).margin({ top: 5, left: 3 })
|
||||||
}.width('100%').height(60).backgroundColor(Color.Pink)
|
}.width('100%').height(60).backgroundColor(Color.Pink)
|
||||||
|
|
||||||
Text("测试pngj和裁剪").fontSize(15)
|
Text("测试pngj和裁剪").fontSize(15)
|
||||||
|
|
|
@ -46,7 +46,25 @@
|
||||||
"name": "ohos.permission.INTERNET",
|
"name": "ohos.permission.INTERNET",
|
||||||
"usedScene": {
|
"usedScene": {
|
||||||
"abilities": [
|
"abilities": [
|
||||||
"FormAbility"
|
"EntryAbility"
|
||||||
|
],
|
||||||
|
"when": "always"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "ohos.permission.MEDIA_LOCATION",
|
||||||
|
"usedScene": {
|
||||||
|
"abilities": [
|
||||||
|
"EntryAbility"
|
||||||
|
],
|
||||||
|
"when": "always"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "ohos.permission.READ_MEDIA",
|
||||||
|
"usedScene": {
|
||||||
|
"abilities": [
|
||||||
|
"EntryAbility"
|
||||||
],
|
],
|
||||||
"when": "always"
|
"when": "always"
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
"pages/cropImagePage2",
|
"pages/cropImagePage2",
|
||||||
"pages/svgTestCasePage",
|
"pages/svgTestCasePage",
|
||||||
"pages/gifTestCasePage",
|
"pages/gifTestCasePage",
|
||||||
"pages/imageknifeTestCaseIndex"
|
"pages/imageknifeTestCaseIndex",
|
||||||
|
"pages/dataShareUriLoadPage"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,12 +19,14 @@ import { Md5 } from '../../cache/Md5'
|
||||||
import { FileUtils } from '../../cache/FileUtils'
|
import { FileUtils } from '../../cache/FileUtils'
|
||||||
import { NetworkDownloadClient } from './NetworkDownloadClient'
|
import { NetworkDownloadClient } from './NetworkDownloadClient'
|
||||||
import { LoadLocalFileClient } from './LoadLocalFileClient'
|
import { LoadLocalFileClient } from './LoadLocalFileClient'
|
||||||
|
import { LoadDataShareFileClient } from './LoadDataShareFileClient'
|
||||||
import loadRequest from '@ohos.request';
|
import loadRequest from '@ohos.request';
|
||||||
|
|
||||||
// 数据加载器
|
// 数据加载器
|
||||||
export class DownloadClient implements IDataFetch {
|
export class DownloadClient implements IDataFetch {
|
||||||
private networkClient = new NetworkDownloadClient();
|
private networkClient = new NetworkDownloadClient();
|
||||||
private localFileClient = new LoadLocalFileClient();
|
private localFileClient = new LoadLocalFileClient();
|
||||||
|
private dataShareFileClient = new LoadDataShareFileClient();
|
||||||
|
|
||||||
loadData(request: RequestOption, onCompleteFunction, onErrorFunction) {
|
loadData(request: RequestOption, onCompleteFunction, onErrorFunction) {
|
||||||
if (typeof request.loadSrc == 'string') {
|
if (typeof request.loadSrc == 'string') {
|
||||||
|
@ -32,6 +34,8 @@ export class DownloadClient implements IDataFetch {
|
||||||
.filesDir) || request.loadSrc.startsWith(globalThis.ImageKnife.getImageKnifeContext().cacheDir)) {
|
.filesDir) || request.loadSrc.startsWith(globalThis.ImageKnife.getImageKnifeContext().cacheDir)) {
|
||||||
// 本地沙盒
|
// 本地沙盒
|
||||||
this.localFileClient.loadData(request, onCompleteFunction, onErrorFunction)
|
this.localFileClient.loadData(request, onCompleteFunction, onErrorFunction)
|
||||||
|
}else if(request.loadSrc.startsWith('datashare://')){
|
||||||
|
this.dataShareFileClient.loadData(request, onCompleteFunction, onErrorFunction)
|
||||||
} else {
|
} else {
|
||||||
// 网络下载
|
// 网络下载
|
||||||
this.networkClient.loadData(request, onCompleteFunction, onErrorFunction)
|
this.networkClient.loadData(request, onCompleteFunction, onErrorFunction)
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 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 { IDataFetch } from '../networkmanage/IDataFetch'
|
||||||
|
import { RequestOption } from '../RequestOption'
|
||||||
|
import fs from '@ohos.file.fs';
|
||||||
|
|
||||||
|
|
||||||
|
export class LoadDataShareFileClient implements IDataFetch {
|
||||||
|
loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) {
|
||||||
|
if (typeof request.loadSrc == 'string') {
|
||||||
|
fs.open(request.loadSrc,fs.OpenMode.READ_ONLY).then((file)=>{
|
||||||
|
let stat = fs.statSync(file.fd);
|
||||||
|
let buf = new ArrayBuffer(stat.size);
|
||||||
|
fs.read(file.fd, buf).then((readLen)=>{
|
||||||
|
onComplete(buf);
|
||||||
|
fs.close(file.fd);
|
||||||
|
}).catch(err=>{
|
||||||
|
onError('LoadDataShareFileClient fs.read err happend uri='+request.loadSrc+" err.msg="+err.message+" err.code="+err.code)
|
||||||
|
})
|
||||||
|
}).catch(err=>{
|
||||||
|
onError('LoadDataShareFileClient fs.open err happend uri='+request.loadSrc+" err.msg="+err.message+" err.code="+err.code)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,7 +19,7 @@ import { Md5 } from '../../cache/Md5'
|
||||||
import { FileUtils } from '../../cache/FileUtils'
|
import { FileUtils } from '../../cache/FileUtils'
|
||||||
import loadRequest from '@ohos.request';
|
import loadRequest from '@ohos.request';
|
||||||
|
|
||||||
export class LoadLocalFileClient {
|
export class LoadLocalFileClient implements IDataFetch {
|
||||||
loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) {
|
loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) {
|
||||||
if (typeof request.loadSrc == 'string') {
|
if (typeof request.loadSrc == 'string') {
|
||||||
let fileBuffer = FileUtils.getInstance().readFilePic(request.loadSrc)
|
let fileBuffer = FileUtils.getInstance().readFilePic(request.loadSrc)
|
||||||
|
|
Loading…
Reference in New Issue