From b65871be9aba5aeed2e4c6c2d3486b7ae7d37d9f Mon Sep 17 00:00:00 2001 From: yuandongping Date: Sat, 10 May 2025 11:46:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=BF=E9=97=AE=E5=85=AC?= =?UTF-8?q?=E5=85=B1=E8=B7=AF=E5=BE=84=E4=B8=8B=E5=9B=BE=E7=89=87=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yuandongping --- .../MindSporeLiteArkTSDemo/README_zh.md | 2 +- .../entry/src/main/ets/pages/Index.ets | 32 +++++++++++++++++++ .../entry/src/main/module.json5 | 11 ++++++- .../main/resources/base/element/string.json | 4 +++ .../main/resources/en_US/element/string.json | 4 +++ .../main/resources/zh_CN/element/string.json | 4 +++ .../src/ohosTest/ets/test/Ability.test.ets | 24 +++++++++++++- .../resources/base/element/string.json | 4 +++ .../MindSporeLiteCDemo/README_zh.md | 2 +- .../entry/src/main/ets/pages/Index.ets | 31 ++++++++++++++++++ .../entry/src/main/module.json5 | 11 ++++++- .../main/resources/base/element/string.json | 4 +++ .../main/resources/en_US/element/string.json | 4 +++ .../main/resources/zh_CN/element/string.json | 4 +++ .../src/ohosTest/ets/test/Ability.test.ets | 24 +++++++++++++- .../resources/base/element/string.json | 4 +++ 16 files changed, 163 insertions(+), 6 deletions(-) diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/README_zh.md b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/README_zh.md index fb38a1bdc3..2b0a8c1723 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/README_zh.md +++ b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/README_zh.md @@ -48,7 +48,7 @@ entry ### 相关权限 -无。 +ohos.permission.READ_IMAGEVIDEO ### 依赖 diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/ets/pages/Index.ets b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/ets/pages/Index.ets index c052b04a10..db05e70737 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/ets/pages/Index.ets +++ b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/ets/pages/Index.ets @@ -1,9 +1,28 @@ +/* +* Copyright (c) 2024-2025 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 modelPredict from './model'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; import { BusinessError } from '@ohos.base'; import image from '@ohos.multimedia.image'; import { resourceManager } from '@kit.LocalizationKit' import { fileIo } from '@kit.CoreFileKit'; +import { abilityAccessCtrl, Permissions } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; + +const PERMISSIONS: Permissions[] = ['ohos.permission.READ_IMAGEVIDEO']; @Entry @Component @@ -101,6 +120,19 @@ struct Index { 'Jewellery', 'Bridge', 'Cuisine', 'Bread', 'Caving', 'Shell', 'Wreath', 'Roof', 'Cookie', 'Canoe']; + aboutToAppear() { + abilityAccessCtrl.createAtManager().requestPermissionsFromUser(getContext(this), PERMISSIONS).then((data) => { + if (data.authResults[0] == 0) { + hilog.info(0xFF00, 'MindSporeLiteArkTSDemo', '%{public}s', 'request permission success'); + } else { + hilog.info(0xFF00, 'MindSporeLiteArkTSDemo', '%{public}s', 'user rejected'); + } + }).catch((err: BusinessError) => { + hilog.error(0xFF00, 'MindSporeLiteArkTSDemo', '%{public}s', + `request permission failed, error message: ${(err as BusinessError).message}`); + }); + } + build() { Row() { Column() { diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/module.json5 b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/module.json5 index 546532a6eb..fcd2d37ece 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/module.json5 +++ b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/module.json5 @@ -1,5 +1,5 @@ /* -* Copyright (c) 2024 Huawei Device Co., Ltd. +* Copyright (c) 2024-2025 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 @@ -47,6 +47,15 @@ } ] } + ], + "requestPermissions": [ + { + "name": "ohos.permission.READ_IMAGEVIDEO", + "reason": "$string:read_image_video_permission", + "usedScene": { + "when": "inuse" + } + } ] } } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/base/element/string.json b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/base/element/string.json index ad88d0d90f..3923bded3d 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/base/element/string.json +++ b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/base/element/string.json @@ -11,6 +11,10 @@ { "name": "EntryAbility_label", "value": "MindSporeLite" + }, + { + "name": "read_image_video_permission", + "value": "读取用户公共目录的图片或视频文件" } ] } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/en_US/element/string.json b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/en_US/element/string.json index ad88d0d90f..add6e500db 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/en_US/element/string.json +++ b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/en_US/element/string.json @@ -11,6 +11,10 @@ { "name": "EntryAbility_label", "value": "MindSporeLite" + }, + { + "name": "read_image_video_permission", + "value": "read image or video files from the user's public directory" } ] } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/zh_CN/element/string.json b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/zh_CN/element/string.json index 4831b337e0..f849a3e633 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/zh_CN/element/string.json +++ b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/main/resources/zh_CN/element/string.json @@ -11,6 +11,10 @@ { "name": "EntryAbility_label", "value": "MindSporeLite" + }, + { + "name": "read_image_video_permission", + "value": "读取用户公共目录的图片或视频文件" } ] } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/ohosTest/ets/test/Ability.test.ets b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/ohosTest/ets/test/Ability.test.ets index 07e0d607c0..743b733fa3 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/ohosTest/ets/test/Ability.test.ets +++ b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/ohosTest/ets/test/Ability.test.ets @@ -1,3 +1,18 @@ +/* +* Copyright (c) 2024-2025 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 { hilog } from '@kit.PerformanceAnalysisKit'; import { describe, it, expect } from '@ohos/hypium'; import Want from '@ohos.app.ability.Want'; @@ -35,6 +50,13 @@ export default function abilityTest() { expect(err).assertNull(); }) await driver.delayMs(3000); + // 用户授权 + await driver.assertComponentExist(ON.text( + await abilityDelegator.getAppContext().resourceManager.getStringValue($r('app.string.allow')))); + let permissionAllowBtn = await driver.findComponent(ON.text( + await abilityDelegator.getAppContext().resourceManager.getStringValue($r('app.string.allow')))); + await permissionAllowBtn.click(); + await driver.delayMs(1000); hilog.info(domain, TAG, BUNDLE + 'EntryAbility_001 end'); done(); }) @@ -72,7 +94,7 @@ export default function abilityTest() { await driver.assertComponentExist(ON.text('photo')); let msPhoto = await driver.findComponent(ON.text('photo')); await msPhoto.click(); - await driver.delayMs(1000); + await driver.delayMs(5000); // 点击照片,选中一张照片 let gridItems = await driver.findComponents(ON.type('GridItem')); await gridItems[1].click(); diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/ohosTest/resources/base/element/string.json b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/ohosTest/resources/base/element/string.json index 6b9a3b4953..c3f097d6be 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/ohosTest/resources/base/element/string.json +++ b/code/DocsSample/ApplicationModels/MindSporeLiteArkTSDemo/entry/src/ohosTest/resources/base/element/string.json @@ -15,6 +15,10 @@ { "name": "completed", "value": "完成" + }, + { + "name": "allow", + "value": "允许" } ] } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/README_zh.md b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/README_zh.md index 4883ca6332..23d9dd4a55 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/README_zh.md +++ b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/README_zh.md @@ -57,7 +57,7 @@ entry ### 相关权限 -无。 +ohos.permission.READ_IMAGEVIDEO ### 依赖 diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/ets/pages/Index.ets b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/ets/pages/Index.ets index c79d4671ab..3beb044cd4 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/ets/pages/Index.ets +++ b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/ets/pages/Index.ets @@ -1,11 +1,29 @@ +/* +* Copyright (c) 2024-2025 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 resourceManager from '@ohos.resourceManager' import fileIo from '@ohos.file.fs'; import msliteNapi from 'libentry.so' import { photoAccessHelper } from '@kit.MediaLibraryKit'; import { BusinessError } from '@ohos.base'; import image from '@ohos.multimedia.image'; +import { abilityAccessCtrl, Permissions } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; const TAG = 'MindSporeLite'; +const PERMISSIONS: Permissions[] = ['ohos.permission.READ_IMAGEVIDEO']; @Entry @Component @@ -104,6 +122,19 @@ struct Index { 'Shell', 'Wreath', 'Roof', 'Cookie', 'Canoe' ]; + aboutToAppear() { + abilityAccessCtrl.createAtManager().requestPermissionsFromUser(getContext(this), PERMISSIONS).then((data) => { + if (data.authResults[0] == 0) { + hilog.info(0xFF00, TAG, '%{public}s', 'request permission success'); + } else { + hilog.info(0xFF00, TAG, '%{public}s', 'user rejected'); + } + }).catch((err: BusinessError) => { + hilog.error(0xFF00, TAG, '%{public}s', + `request permission failed, error message: ${(err as BusinessError).message}`); + }); + } + build() { Row() { Column() { diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/module.json5 b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/module.json5 index 546532a6eb..fcd2d37ece 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/module.json5 +++ b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/module.json5 @@ -1,5 +1,5 @@ /* -* Copyright (c) 2024 Huawei Device Co., Ltd. +* Copyright (c) 2024-2025 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 @@ -47,6 +47,15 @@ } ] } + ], + "requestPermissions": [ + { + "name": "ohos.permission.READ_IMAGEVIDEO", + "reason": "$string:read_image_video_permission", + "usedScene": { + "when": "inuse" + } + } ] } } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/base/element/string.json b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/base/element/string.json index 635edd86c2..94723c4c5a 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/base/element/string.json +++ b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/base/element/string.json @@ -15,6 +15,10 @@ { "name": "completed", "value": "完成" + }, + { + "name": "read_image_video_permission", + "value": "读取用户公共目录的图片或视频文件" } ] } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/en_US/element/string.json b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/en_US/element/string.json index 20a6d90b23..e589ea278b 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/en_US/element/string.json +++ b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/en_US/element/string.json @@ -15,6 +15,10 @@ { "name": "completed", "value": "completed" + }, + { + "name": "read_image_video_permission", + "value": "read image or video files from the user's public directory" } ] } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/zh_CN/element/string.json b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/zh_CN/element/string.json index 3935b55bd7..b7a0c90491 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/zh_CN/element/string.json +++ b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/main/resources/zh_CN/element/string.json @@ -15,6 +15,10 @@ { "name": "completed", "value": "完成" + }, + { + "name": "read_image_video_permission", + "value": "读取用户公共目录的图片或视频文件" } ] } \ No newline at end of file diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/ohosTest/ets/test/Ability.test.ets b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/ohosTest/ets/test/Ability.test.ets index efd60bda6a..18c0d12f2c 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/ohosTest/ets/test/Ability.test.ets +++ b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/ohosTest/ets/test/Ability.test.ets @@ -1,3 +1,18 @@ +/* +* Copyright (c) 2024-2025 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 { hilog } from '@kit.PerformanceAnalysisKit'; import { describe, it, expect } from '@ohos/hypium'; import Want from '@ohos.app.ability.Want'; @@ -35,6 +50,13 @@ export default function abilityTest() { expect(err).assertNull(); }) await driver.delayMs(3000); + // 用户授权 + await driver.assertComponentExist(ON.text( + await abilityDelegator.getAppContext().resourceManager.getStringValue($r('app.string.allow')))); + let permissionAllowBtn = await driver.findComponent(ON.text( + await abilityDelegator.getAppContext().resourceManager.getStringValue($r('app.string.allow')))); + await permissionAllowBtn.click(); + await driver.delayMs(1000); hilog.info(DOMAIN, TAG, BUNDLE + 'EntryAbility_001 end'); done(); }) @@ -72,7 +94,7 @@ export default function abilityTest() { await driver.assertComponentExist(ON.text('photo')); let msPhoto = await driver.findComponent(ON.text('photo')); await msPhoto.click(); - await driver.delayMs(1000); + await driver.delayMs(5000); // chose a photo let gridItems = await driver.findComponents(ON.type('GridItem')); await gridItems[1].click(); diff --git a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/ohosTest/resources/base/element/string.json b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/ohosTest/resources/base/element/string.json index 6b9a3b4953..c3f097d6be 100644 --- a/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/ohosTest/resources/base/element/string.json +++ b/code/DocsSample/ApplicationModels/MindSporeLiteCDemo/entry/src/ohosTest/resources/base/element/string.json @@ -15,6 +15,10 @@ { "name": "completed", "value": "完成" + }, + { + "name": "allow", + "value": "允许" } ] } \ No newline at end of file -- Gitee