1# 自定义分享 2 3### 介绍 4 5自定义分享主要是发送方将文本,链接,图片三种类型分享给三方应用,同时能够在三方应用中展示。本示例使用[数据请求](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-http.md) 6实现网络资源的获取,使用[屏幕截屏](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-screenshot.md) 7实现屏幕的截取,使用[文件管理](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-fileio.md) 8实现对文件,文件目录的管理,使用[媒体库管理](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-medialibrary.md) 9获取截取的图片,使用[弹窗](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-promptAction.md) 10进行信息的提示。 11 12### 效果预览 13 14| 主页 | 点击分享 | 分享页面 | 15|----------------------------------|-------------------------------------------|-------------------------------------------| 16|  |  |  | 17| **其它应用分享** | **文本分享成功预览页** | **文本编辑成功预览页** | 18|  |  |  | 19 20使用说明: 21 221.启动Share应用,首页展示分享跳转页面,点击分享按钮出现自定义分享弹窗(三方APP的)。 23 242.点击分享文本+链接,选择"Chat"图标,会拉起三方应用[Chat](https://gitee.com/openharmony/applications_app_samples/tree/master/code/Solutions/IM/Chat) 25,此时选择[聊天列表](entry/src/main/ets/pages/Index.ets) 26中任意的朋友进行分享。 27 283.点击生成海报,此时第一层弹窗消失并出现新的弹窗,点击截图并分享,会出现小提示"截图成功,分享加载中..." 29,稍后会拉起三方应用[Chat](https://gitee.com/openharmony/applications_app_samples/tree/master/code/Solutions/IM/Chat) 30,此时选择[聊天列表](entry/src/main/ets/pages/Index.ets) 31中任意的朋友进行分享。 32 334.安装[entry](./entry/)以及[textShare](./textShare/)两个module的hap包 34,点击分享文本+链接,选择"文本分享"图标,会拉起应用[文本分享](./textShare/) 35,此时选择"留在文本分享"可以进入[文本页面](./textShare/src/main/ets/textreceiveability/TextReceiveAbility.ts) 36,若选择"返回",则会回到Share应用主页面。 37 385.安装[entry](./entry/)以及[textEdit](./textEdit/)两个module的hap包 39,点击分享文本+链接,选择"文本编辑"图标,会拉起应用[文本编辑](./textEdit/) 40,此时选择"留在文本编辑"可以进入[文本编辑页面](./textEdit/src/main/ets/editability/EditTextAbility.ts) 41,若选择"返回",则会回到Share应用主页面。 42 43### 工程目录 44``` 45entry/src/main/ets/ 46|---Application 47| |---MyAbilityStage.ets 48|---MainAbility 49| |---MainAbility.ts 50|---model 51| |---Logger.ts // 日志工具 52|---pages 53| |---Index.ets // 首页 54textShare/src/main/ets/ 55├──model 56| └──Logger.ts 57├──pages 58| ├──Index.ets // TextReceiveAbility页面 59| ├──TemplateBuilder.ets // 自定义组件页面 60| └──ShareExtenIndex.ets // ShareExtension页面 61├──textreceiveability 62| └──TextReceiveAbility.ts // 文本分享主页面 63├──shareextensionability 64| └──ShareExtAbility.ts 65textEdit/src/main/ets/ 66├──editability 67| └──EditTextAbility.ts // 文本编辑主页面 68├──model 69| └──Logger.ts 70├──pages 71| ├──Index.ets // EditTextAbility页面 72| ├──TemplateBuilder.ets // 自定义组件页面 73| └──ShareExtenIndex.ets // ShareExtension页面 74└──shareextensionability 75 └──ShareExtAbility.ts 76``` 77 78### 具体实现 79 80* 本示例分为链接分享,应用内文件分享,其它应用分享三个模块: 81 * 文本+链接分享模块 82 * ButtonDialogBuilder方法展示自定义分享弹窗,@ohos.abilityAccessCtrl接口获取访问控制权限,@ohos.net.http接口获取网络资源。 83 * 源码链接:[MainAbility.ts](entry/src/main/ets/MainAbility/MainAbility.ts),[HttpRequest.ets](ShareComponent/src/main/ets/net/HttpRequest.ets) 84 * 参考接口:[@ohos.prompt](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-promptAction.md),[@ohos.abilityAccessCtrl](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-abilityAccessCtrl.md),[@ohos.net.http](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-http.md) 85 86 * 应用内文件分享模块 87 * 使用弹窗,屏幕截屏(截取屏幕),文件管理(管理文件和文件目录)和媒体库管理(获取截取的图片)接口进行图片、链接,视频的分享 88 * 源码链接:[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) 89 * 参考接口:[@ohos.prompt](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-promptAction.md),[@ohos.screenshot](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-screenshot.md),[@ohos.fileio](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-fileio.md),[@ohos.multimedia.mediaLibrary](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-medialibrary.md) 90 91 * 其他应用分享模块: 92 * 使用弹窗,屏幕截屏(截取屏幕),文件管理(管理文件和文件目录)和媒体库管理(获取截取的图片)API,把图片,链接,文件在其他应用内分享 93 * 源码链接:[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) 94 * 参考接口:[@ohos.prompt](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-promptAction.md),[@ohos.screenshot](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-screenshot.md),[@ohos.fileio](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-fileio.md),[@ohos.multimedia.mediaLibrary](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-medialibrary.md) 95 96* 本示例还包含文本分享应用及文本编辑应用: 97 * 在Index.ets中加载TemplateBuilder自定义组件并显示分享信息, 源码参考[textShare_Index.ets](./textShare/src/main/ets/pages/Index.ets). [textEdit_Index.ets](./textEdit/src/main/ets/pages/Index.ets)。 98 * 在加载Index页面中,如果是被分享方拉起,则加载TemplateBuilder自定义组件。若不是被分享拉起,则显示Ability设置内容。 99 100 * TemplateBuilder组件内容封装在TemplateBuilder.ets中,源码参考:[textShare_TemplateBuilder.ets](./textShare/src/main/ets/pages/TemplateBuilder.ets). 101 [textEdit_TemplateBuilder.ets](./textEdit/src/main/ets/pages/TemplateBuilder.ets)。 102 * 在TemplateBuilder组件中包含ShareExtAbility组件、“返回Share”和“留在文本分享/留在文本编辑”按钮,可选择返回Share应用或留在当前(文本分享/文本编辑)应用。 103 104### 相关权限 105 106允许应用截取屏幕图像:[ohos.permission.CAPTURE_SCREEN](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/permission-list.md#ohospermissioncapture_screen) 107 108允许使用Internet网络:[ohos.permission.INTERNET](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/permission-list.md#ohospermissioninternet) 109 110允许应用访问用户媒体文件中的地理位置信息:[ohos.permission.MEDIA_LOCATION](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/permission-list.md#ohospermissionmedia_location) 111 112允许应用读取用户外部存储中的媒体文件信息:[ohos.permission.READ_MEDIA](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/permission-list.md#ohospermissionread_media) 113 114允许应用读写用户外部存储中的媒体文件信息:[ohos.permission.WRITE_MEDIA](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/permission-list.md#ohospermissionwrite_media) 115 116### 依赖 117 118依赖于[Chat](https://gitee.com/openharmony/applications_app_samples/tree/master/code/Solutions/IM/Chat) 应用,来作为接收方。 119 120entry中测试[Share.test.ets](./entry/src/ohosTest/ets/test/Share.test.ets)需要依赖[textShare](./textShare/)以及[textEdit](./textEdit/),需要编译两个feature module的hap包,并与entry应用hap包及测试hap包一同签名并安装。 121 122### 约束与限制 123 1241.本示例仅支持标准系统上运行,支持设备:RK3568。 125 1262.本示例为Stage模型,支持API10版本SDK,SDK版本号(API Version 10 Release),镜像版本号(4.0 Release) 127 1283.本示例需要使用DevEco Studio 版本号(4.0 Release)及以上版本才可编译运行 129 1304.本示例使用的screenshot API属于SystemAPI,需要使用Full SDK 手动从镜像站点获取,并在DevEco 131Studio中替换,具体操作可参考[替换指南](https://docs.openharmony.cn/pages/v3.2/zh-cn/application-dev/quick-start/full-sdk-switch-guide.md/)。 132 1335.本示例涉及相关权限为system_core级别(相关权限级别可通过[权限定义列表](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/permission-list.md) 134查看), 需要手动配置高级别的权限签名( 135具体操作可查看[自动化签名方案](https://docs.openharmony.cn/pages/v3.2/zh-cn/application-dev/security/hapsigntool-overview.md/)) 。 136 1376.本示例类型为系统应用,需要手动配置对应级别的应用类型("app-feature": "hos_system_app")。具体可参考profile配置文件[bundle-info对象内部结构]( https://gitee.com/openharmony/docs/blob/eb73c9e9dcdd421131f33bb8ed6ddc030881d06f/zh-cn/application-dev/security/app-provision-structure.md#bundle-info%E5%AF%B9%E8%B1%A1%E5%86%85%E9%83%A8%E7%BB%93%E6%9E%84 ) 138 139### 下载 140 141如需单独下载本工程,执行如下命令: 142 143``` 144git init 145git config core.sparsecheckout true 146echo code/BasicFeature/ApplicationModels/CustomShare/ > .git/info/sparse-checkout 147git remote add origin https://gitee.com/openharmony/applications_app_samples.git 148git pull origin master 149``` 150