diff --git a/entry/src/main/ets/entryability/EntryAbility.ts b/entry/src/main/ets/entryability/EntryAbility.ts index 5589925..0542346 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ts +++ b/entry/src/main/ets/entryability/EntryAbility.ts @@ -3,6 +3,8 @@ import hilog from '@ohos.hilog'; import window from '@ohos.window'; import { ImageKnife,ImageKnifeDrawFactory } from '@ohos/imageknife' import { CustomEngineKeyImpl } from './CustomEngineKeyImpl' +import abilityAccessCtrl,{Permissions} from '@ohos.abilityAccessCtrl'; + export default class EntryAbility extends UIAbility { onCreate(want, launchParam) { @@ -22,6 +24,18 @@ export default class EntryAbility extends UIAbility { // Main window is created, set main page for this ability hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + let list : Array = ['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) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); diff --git a/entry/src/main/ets/pages/dataShareUriLoadPage.ets b/entry/src/main/ets/pages/dataShareUriLoadPage.ets new file mode 100644 index 0000000..20775d4 --- /dev/null +++ b/entry/src/main/ets/pages/dataShareUriLoadPage.ets @@ -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%') + } + +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/imageknifeTestCaseIndex.ets b/entry/src/main/ets/pages/imageknifeTestCaseIndex.ets index f01d186..8cc6451 100644 --- a/entry/src/main/ets/pages/imageknifeTestCaseIndex.ets +++ b/entry/src/main/ets/pages/imageknifeTestCaseIndex.ets @@ -139,6 +139,11 @@ struct IndexFunctionDemo { console.log("重试Retry") router.push({ uri: "pages/frescoRetryTestCasePage" }); }).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) Text("测试pngj和裁剪").fontSize(15) diff --git a/entry/src/main/module.json5 b/entry/src/main/module.json5 index 7071f74..f15f6f5 100644 --- a/entry/src/main/module.json5 +++ b/entry/src/main/module.json5 @@ -46,7 +46,25 @@ "name": "ohos.permission.INTERNET", "usedScene": { "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" } diff --git a/entry/src/main/resources/base/profile/main_pages.json b/entry/src/main/resources/base/profile/main_pages.json index 1d45314..59f4362 100644 --- a/entry/src/main/resources/base/profile/main_pages.json +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -23,6 +23,7 @@ "pages/cropImagePage2", "pages/svgTestCasePage", "pages/gifTestCasePage", - "pages/imageknifeTestCaseIndex" + "pages/imageknifeTestCaseIndex", + "pages/dataShareUriLoadPage" ] } diff --git a/imageknife/src/main/ets/components/imageknife/networkmanage/DownloadClient.ets b/imageknife/src/main/ets/components/imageknife/networkmanage/DownloadClient.ets index 485f1b3..4e1589b 100644 --- a/imageknife/src/main/ets/components/imageknife/networkmanage/DownloadClient.ets +++ b/imageknife/src/main/ets/components/imageknife/networkmanage/DownloadClient.ets @@ -19,12 +19,14 @@ import { Md5 } from '../../cache/Md5' import { FileUtils } from '../../cache/FileUtils' import { NetworkDownloadClient } from './NetworkDownloadClient' import { LoadLocalFileClient } from './LoadLocalFileClient' +import { LoadDataShareFileClient } from './LoadDataShareFileClient' import loadRequest from '@ohos.request'; // 数据加载器 export class DownloadClient implements IDataFetch { private networkClient = new NetworkDownloadClient(); private localFileClient = new LoadLocalFileClient(); + private dataShareFileClient = new LoadDataShareFileClient(); loadData(request: RequestOption, onCompleteFunction, onErrorFunction) { if (typeof request.loadSrc == 'string') { @@ -32,6 +34,8 @@ export class DownloadClient implements IDataFetch { .filesDir) || request.loadSrc.startsWith(globalThis.ImageKnife.getImageKnifeContext().cacheDir)) { // 本地沙盒 this.localFileClient.loadData(request, onCompleteFunction, onErrorFunction) + }else if(request.loadSrc.startsWith('datashare://')){ + this.dataShareFileClient.loadData(request, onCompleteFunction, onErrorFunction) } else { // 网络下载 this.networkClient.loadData(request, onCompleteFunction, onErrorFunction) diff --git a/imageknife/src/main/ets/components/imageknife/networkmanage/LoadDataShareFileClient.ets b/imageknife/src/main/ets/components/imageknife/networkmanage/LoadDataShareFileClient.ets new file mode 100644 index 0000000..ba73295 --- /dev/null +++ b/imageknife/src/main/ets/components/imageknife/networkmanage/LoadDataShareFileClient.ets @@ -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) + }) + } + } +} \ No newline at end of file diff --git a/imageknife/src/main/ets/components/imageknife/networkmanage/LoadLocalFileClient.ets b/imageknife/src/main/ets/components/imageknife/networkmanage/LoadLocalFileClient.ets index 4998b72..f4df6bf 100644 --- a/imageknife/src/main/ets/components/imageknife/networkmanage/LoadLocalFileClient.ets +++ b/imageknife/src/main/ets/components/imageknife/networkmanage/LoadLocalFileClient.ets @@ -19,7 +19,7 @@ import { Md5 } from '../../cache/Md5' import { FileUtils } from '../../cache/FileUtils' import loadRequest from '@ohos.request'; -export class LoadLocalFileClient { +export class LoadLocalFileClient implements IDataFetch { loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) { if (typeof request.loadSrc == 'string') { let fileBuffer = FileUtils.getInstance().readFilePic(request.loadSrc)