diff --git a/code/SystemFeature/ApplicationModels/CustomShare/README_zh.md b/code/SystemFeature/ApplicationModels/CustomShare/README_zh.md index f3b15d5fceff8c7dd6162b99b012f35996b2a859..aa812a56f08bede7f847511a468992ec79f74bfb 100644 --- a/code/SystemFeature/ApplicationModels/CustomShare/README_zh.md +++ b/code/SystemFeature/ApplicationModels/CustomShare/README_zh.md @@ -2,8 +2,8 @@ ### 介绍 -自定义分享主要是发送方将文本,链接,图片三种类型分享给三方应用,同时能够在三方应用中展示。本示例使用[数据请求](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-http.md) -实现网络资源的获取,使用[屏幕截屏](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-screenshot-sys.md)实现屏幕的截取,使用[文件管理](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-core-file-kit/js-apis-fileio.md)实现对文件,文件目录的管理,使用[媒体库管理](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-media-library-kit/js-apis-medialibrary-sys.md)获取截取的图片,使用[弹窗](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-prompt.md)进行信息的提示。 +自定义分享主要是发送方将文本,链接,图片三种类型分享给三方应用,同时能够在三方应用中展示。本示例使用[数据请求](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-network-kit/js-apis-http.md) +实现网络资源的获取,使用[屏幕截屏](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-screenshot-sys.md)实现屏幕的截取,使用[文件管理](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-core-file-kit/js-apis-fileio.md)实现对文件,文件目录的管理,使用[相册管理](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-media-library-kit/js-apis-photoAccessHelper.md)获取截取的图片,使用[弹窗](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-prompt.md)进行信息的提示。 ### 效果预览 @@ -18,10 +18,10 @@ 1.启动Share应用,首页展示分享跳转页面,点击分享按钮出现自定义分享弹窗(三方APP的)。 2.点击分享文本+链接,选择"Chat"图标,会拉起三方应用[Chat](https://gitee.com/openharmony/applications_app_samples/tree/master/code/Solutions/IM/Chat),此时选择[聊天列表](entry/src/main/ets/pages/Index.ets)中任意的朋友进行分享。 - + 3.点击生成海报,此时第一层弹窗消失并出现新的弹窗,点击截图并分享,会出现小提示"截图成功,分享加载中...",稍后会拉起三方应用[Chat](https://gitee.com/openharmony/applications_app_samples/tree/master/code/Solutions/IM/Chat),此时选择[聊天列表](entry/src/main/ets/pages/Index.ets)中任意的朋友进行分享。 -4.安装[entry](./entry/)以及[textShare](./textShare/)两个module的hap包,点击分享文本+链接,选择"文本分享"图标,会拉起应用[文本分享](./textShare/),此时选择"留在文本分享"可以进入[文本页面](./textShare/src/main/ets/textreceiveability/TextReceiveAbility.ts),若选择"返回",则会回到Share应用主页面。 +4.安装[entry](./entry/)以及[textShare](./text Share/)两个module的hap包,点击分享文本+链接,选择"文本分享"图标,会拉起应用[文本分享](./textShare/),此时选择"留在文本分享"可以进入[文本页面](./textShare/src/main/ets/textreceiveability/TextReceiveAbility.ts),若选择"返回",则会回到Share应用主页面。 5.安装[entry](./entry/)以及[textEdit](./textEdit/)两个module的hap包,点击分享文本+链接,选择"文本编辑"图标,会拉起应用[文本编辑](./textEdit/),此时选择"留在文本编辑"可以进入[文本编辑页面](./textEdit/src/main/ets/editability/EditTextAbility.ts),若选择"返回",则会回到Share应用主页面。 @@ -71,12 +71,12 @@ textEdit/src/main/ets/ * 应用内文件分享模块 * 使用弹窗,屏幕截屏(截取屏幕),文件管理(管理文件和文件目录)和媒体库管理(获取截取的图片)接口进行图片、链接,视频的分享。 * 源码链接:[ShareUtils.ts](ShareComponent/src/main/ets/feature/ShareUtils.ts),[MediaUtils.ts](ShareComponent/src/main/ets/feature/MediaUtils.ts),[ShareConst.ts](ShareComponent/src/main/ets/util/ShareConst.ts) - * 参考接口:[@ohos.prompt](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-prompt.md),[@ohos.screenshot](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-screenshot-sys.md),[@ohos.fileio](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-core-file-kit/js-apis-fileio.md),[@ohos.multimedia.mediaLibrary](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-media-library-kit/js-apis-medialibrary-sys.md) + * 参考接口:[@ohos.prompt](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-prompt.md),[@ohos.screenshot](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-screenshot-sys.md),[@ohos.fileio](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-core-file-kit/js-apis-fileio.md),[@ohos.file.photoAccessHelper](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-media-library-kit/js-apis-photoAccessHelper.md) * 其他应用分享模块: * 使用弹窗,屏幕截屏(截取屏幕),文件管理(管理文件和文件目录)和媒体库管理(获取截取的图片)API,把图片,链接,文件在其他应用内分享 * 源码链接:[ShareUtils.ts](ShareComponent/src/main/ets/feature/ShareUtils.ts),[MediaUtils.ts](ShareComponent/src/main/ets/feature/MediaUtils.ts),[ShareConst.ts](ShareComponent/src/main/ets/util/ShareConst.ts) - * 参考接口:[@ohos.prompt](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-prompt.md),[@ohos.screenshot](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-screenshot-sys.md),[@ohos.fileio](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-core-file-kit/js-apis-fileio.md),[@ohos.multimedia.mediaLibrary](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-media-library-kit/js-apis-medialibrary-sys.md) + * 参考接口:[@ohos.prompt](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-prompt.md),[@ohos.screenshot](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-screenshot-sys.md),[@ohos.fileio](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-core-file-kit/js-apis-fileio.md),[@ohos.file.photoAccessHelper](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-media-library-kit/js-apis-photoAccessHelper.md) * 本示例还包含文本分享应用及文本编辑应用: * 在Index.ets中加载TemplateBuilder自定义组件并显示分享信息, 源码参考[textShare_Index.ets](./textShare/src/main/ets/pages/Index.ets). [textEdit_Index.ets](./textEdit/src/main/ets/pages/Index.ets)。 @@ -94,9 +94,6 @@ textEdit/src/main/ets/ 允许应用访问用户媒体文件中的地理位置信息:[ohos.permission.MEDIA_LOCATION](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissionmedia_location) -允许应用读取用户外部存储中的媒体文件信息:[ohos.permission.READ_MEDIA](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissionread_media) - -允许应用读写用户外部存储中的媒体文件信息:[ohos.permission.WRITE_MEDIA](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissionwrite_media) ### 依赖 @@ -107,7 +104,7 @@ entry中测试[Share.test.ets](./entry/src/ohosTest/ets/test/Share.test.ets)需 ### 约束与限制 1.本示例仅支持标准系统上运行,支持设备:RK3568。 - + 2.本示例为Stage模型,支持API10版本SDK,SDK版本号(API Version 10 Release),镜像版本号(4.0 Release)。 3.本示例需要使用DevEco Studio 版本号(4.0 Release)及以上版本才可编译运行。 diff --git a/code/SystemFeature/ApplicationModels/CustomShare/ShareComponent/src/main/ets/feature/MediaUtils.ts b/code/SystemFeature/ApplicationModels/CustomShare/ShareComponent/src/main/ets/feature/MediaUtils.ts index 9e6b258024d44709bdbae09f240cb52a006f8a0f..9c1dbd424762e01a7c4a1723757aedd1975cd0d5 100644 --- a/code/SystemFeature/ApplicationModels/CustomShare/ShareComponent/src/main/ets/feature/MediaUtils.ts +++ b/code/SystemFeature/ApplicationModels/CustomShare/ShareComponent/src/main/ets/feature/MediaUtils.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -13,29 +13,35 @@ * limitations under the License. */ -import mediaLibrary from '@ohos.multimedia.mediaLibrary' +import photoAccessHelper from '@ohos.file.photoAccessHelper'; +import dataSharePredicates from '@ohos.data.dataSharePredicates'; import Logger from '../util/Logger' const TAG: string = 'MediaUtils' class MediaUtils { async getFileAssetsFromType(mediaType: number, context: any) { - let mediaList: Array = [] - let mediaLib: mediaLibrary.MediaLibrary = mediaLibrary.getMediaLibrary(context) - Logger.info(TAG, `getFileAssetsFromType,mediaType: ${mediaType}`) - let fileKeyObj = mediaLibrary.FileKey - - let fetchOption = { - selections: `${fileKeyObj.MEDIA_TYPE}=?`, - selectionArgs: [`${mediaType}`] - } - + let mediaList: Array = []; + let mediaLib: photoAccessHelper.PhotoAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); + Logger.info(TAG, `getFileAssetsFromType,mediaType: ${mediaType}`); // 获取文件资源 - let fetchFileResult = await mediaLib.getFileAssets(fetchOption) - Logger.info(TAG, `getFileAssetsFromType,fetchFileResult.count: ${fetchFileResult.getCount()}`) - // getCount 获取文件检索结果中的文件总数。 - if (fetchFileResult.getCount() > 0) { - mediaList = await fetchFileResult.getAllObject() // 获取文件检索结果中的所有文件资产。此方法返回FileAsset结果集。 + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [`${mediaType}`], + predicates: predicates + }; + try { + let fetchResult: photoAccessHelper.FetchResult = await mediaLib.getAssets(fetchOptions); + if (fetchResult !== undefined) { + Logger.info(TAG, 'fetchResult success'); + Logger.info(TAG, `getFileAssetsFromType,fetchFileResult.count: ${fetchResult.getCount()}`) + // getCount 获取文件检索结果中的文件总数。 + if (fetchResult.getCount() > 0) { + mediaList = await fetchResult.getAllObjects(); // 获取文件检索结果中的所有文件资产。此方法返回FileAsset结果集。 + } + } + } catch (err) { + Logger.error(TAG,`getAssets failed, error: ${err.code}, ${err.message}`); } return mediaList } diff --git a/code/SystemFeature/ApplicationModels/CustomShare/ShareComponent/src/main/ets/feature/ShareUtils.ts b/code/SystemFeature/ApplicationModels/CustomShare/ShareComponent/src/main/ets/feature/ShareUtils.ts index d0c0aaa79e92e6d88a48a1a364de5c606382e43b..98c7907f450bc2243e8e5ded051b21e7ef975286 100644 --- a/code/SystemFeature/ApplicationModels/CustomShare/ShareComponent/src/main/ets/feature/ShareUtils.ts +++ b/code/SystemFeature/ApplicationModels/CustomShare/ShareComponent/src/main/ets/feature/ShareUtils.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -18,7 +18,7 @@ import fileio from '@ohos.fileio' import display from '@ohos.display' import screenshot from '@ohos.screenshot' import image from '@ohos.multimedia.image' -import mediaLibrary from '@ohos.multimedia.mediaLibrary' +import photoAccessHelper from '@ohos.file.photoAccessHelper'; import Logger from '../util/Logger' import MediaUtils from '../feature/MediaUtils' import { ShareConst } from '../util/ShareConst' @@ -209,10 +209,10 @@ export class ShareUtils { } } - async queryMedia(mediaList: Array, fileAssetName: string) { + async queryMedia(mediaList: Array, fileAssetName: string) { let fdNum: number for (let i = 0; i < mediaList.length; i++) { - Logger.info(TAG, `queryMedia: file.id: ${mediaList[i].id}, file.name: ${mediaList[i].displayName}`) + Logger.info(TAG, `queryMedia: file.uri: ${mediaList[i].uri}, file.name: ${mediaList[i].displayName}`) try { if (mediaList[i].displayName === fileAssetName) { let fileAsset = mediaList[i] @@ -228,11 +228,11 @@ export class ShareUtils { } async getFileData(contextCaller) { - let mediaList: Array = [] - let fileAssets = MediaUtils.getFileAssetsFromType(mediaLibrary.MediaType.VIDEO, this.context) + let mediaList: Array = []; + let fileAssets = MediaUtils.getFileAssetsFromType(photoAccessHelper.PhotoType.VIDEO, this.context); let fileAssetName: string = 'video_4.mp4' let fdVideo - mediaList = await MediaUtils.getFileAssetsFromType(mediaLibrary.MediaType.VIDEO, this.context) + mediaList = await MediaUtils.getFileAssetsFromType(photoAccessHelper.PhotoType.VIDEO, this.context); Logger.info(TAG, `getFileData: getFileData mediaList: ${mediaList}`) fdVideo = await this.queryMedia(mediaList, fileAssetName) Logger.info(TAG, `getFileData: getFileData fd: ${fdVideo}`) diff --git a/code/SystemFeature/ApplicationModels/CustomShare/entry/src/main/ets/MainAbility/MainAbility.ts b/code/SystemFeature/ApplicationModels/CustomShare/entry/src/main/ets/MainAbility/MainAbility.ts index 9971deb92df14a59fb705ed579fc1e2271e28e70..8d6f306ad80b2a7447b6ef66b8a25ddb08099035 100644 --- a/code/SystemFeature/ApplicationModels/CustomShare/entry/src/main/ets/MainAbility/MainAbility.ts +++ b/code/SystemFeature/ApplicationModels/CustomShare/entry/src/main/ets/MainAbility/MainAbility.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -24,7 +24,7 @@ export default class MainAbility extends UIAbility { Logger.info(TAG, `MainAbility onCreate`) let atManager = abilityAccessCtrl.createAtManager() try { - atManager.requestPermissionsFromUser(this.context,['ohos.permission.CAPTURE_SCREEN','ohos.permission.READ_MEDIA','ohos.permission.WRITE_MEDIA','ohos.permission.MEDIA_LOCATION','ohos.permission.INTERNET']).then((data) => { + atManager.requestPermissionsFromUser(this.context,['ohos.permission.CAPTURE_SCREEN','ohos.permission.MEDIA_LOCATION','ohos.permission.INTERNET']).then((data) => { Logger.info(TAG, `data: ${JSON.stringify(data)}`) }).catch((err) => { Logger.info(TAG, `err: ${JSON.stringify(err)}`) diff --git a/code/SystemFeature/ApplicationModels/CustomShare/entry/src/main/module.json5 b/code/SystemFeature/ApplicationModels/CustomShare/entry/src/main/module.json5 index 27800631b67bb792b0afe994539bdab3357396f4..c4979477e1b9c5d73421c01fb6e6fa7ead71a9c6 100644 --- a/code/SystemFeature/ApplicationModels/CustomShare/entry/src/main/module.json5 +++ b/code/SystemFeature/ApplicationModels/CustomShare/entry/src/main/module.json5 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -61,26 +61,6 @@ "when": "inuse" } }, - { - "name": "ohos.permission.READ_MEDIA", - "reason": "$string:permission_read_media", - "usedScene": { - "abilities": [ - "MainAbility" - ], - "when": "inuse" - } - }, - { - "name": "ohos.permission.WRITE_MEDIA", - "reason": "$string:permission_write_media", - "usedScene": { - "abilities": [ - "MainAbility" - ], - "when": "inuse" - } - }, { "name": "ohos.permission.MEDIA_LOCATION", "reason": "$string:permission_media_location", diff --git a/code/SystemFeature/ApplicationModels/CustomShare/entry/src/main/resources/base/element/string.json b/code/SystemFeature/ApplicationModels/CustomShare/entry/src/main/resources/base/element/string.json index 0c165d07095e7789221f4a0090db6518f63a8c58..59b125858e9da8049cc492b15198951904a527bf 100644 --- a/code/SystemFeature/ApplicationModels/CustomShare/entry/src/main/resources/base/element/string.json +++ b/code/SystemFeature/ApplicationModels/CustomShare/entry/src/main/resources/base/element/string.json @@ -48,14 +48,6 @@ "name": "permission_internet", "value": "允许使用Internet网络" }, - { - "name": "permission_read_media", - "value": "允许应用读取用户外部存储中的媒体文件信息" - }, - { - "name": "permission_write_media", - "value": "允许应用读写用户外部存储中的媒体文件信息" - }, { "name": "permission_media_location", "value": "允许应用访问用户媒体文件中的地理位置信息"