如何通过ContentHub把内容从一个地方传到另外一个地方
来源:互联网 发布:淘宝互刷红包群 编辑:程序博客网 时间:2024/06/05 07:28
我们知道在Ubuntu手机中一个应用不能直接访问另外一个应用的空间。我们有时需要这么做。比如我们想把我们自己使用照相机API照下一个照片,并放入到我们自己应用的自己可以访问的空间。但是我们不能直接把我们所照的照片直接放入到Gallery应用所拥有的目录中。如果是这样做,直接就违反系统的平台安全性。更多关于平台安全性,可以阅读文章“Ubuntu OS应用Runtime Enviroment”。我们该如何把自己的照片存入到Gallery应用所拥有的目录并让Gallery应用来显示呢?答案是使用ContentHub API。
在桌面系统上,我们必须安装如下的包:
$ sudo apt-get install content-hub$ sudo apt-get install qtdeclarative5-ubuntu-content1
下面我们来通过一个例程来完成展示如何完成这个功能。
首先,我们来完成一个Dialog:
import QtQuick 2.0import Ubuntu.Components 1.1import Ubuntu.Components.Popups 1.0import Ubuntu.Content 1.1PopupBase { id: downloadDialog anchors.fill: parent property var activeTransfer property var downloadId property alias contentType: peerPicker.contentType Rectangle { anchors.fill: parent ContentPeerPicker { id: peerPicker handler: ContentHandler.Destination visible: parent.visible onPeerSelected: { activeTransfer = peer.request() activeTransfer.downloadId = downloadDialog.downloadId activeTransfer.state = ContentTransfer.Downloading PopupUtils.close(downloadDialog) } onCancelPressed: { PopupUtils.close(downloadDialog) } } }}
这个是用来显示一个Dialog。它是让我们选择我们想要放到什么地方去。这里的contentType是让我们选择我们所需要的内容的类型。
import QtQuick 2.0import Ubuntu.Components 1.1import Ubuntu.Components.Popups 1.0import Ubuntu.DownloadManager 0.1import Ubuntu.Content 1.1/*! \brief MainView with a Label and Button elements.*/MainView { // objectName for functional testing purposes (autopilot-qt5) objectName: "mainView" // Note! applicationName needs to match the "name" field of the click manifest applicationName: "contenthub-savefile.liu-xiao-guo" /* This property enables the application to change orientation when the device is rotated. The default is false. */ //automaticOrientation: true // Removes the old toolbar and enables new features of the new header. useDeprecatedToolbar: false width: units.gu(60) height: units.gu(85) Page { id: page title: i18n.tr("contenthub-savefile") Component { id: downloadDialog ContentSaveDialog { } } SingleDownload { id: downloader autoStart: false onDownloadIdChanged: { PopupUtils.open( downloadDialog, page, {"contentType" : ContentType.Pictures, "downloadId" : downloadId } ) } onFinished: { print("download finished! saved path: " + path); } } Column { anchors.centerIn: parent spacing: units.gu(1) Image { id: image source: "images/sample.jpg" width: page.width/2 height: page.height/2 } Button { anchors.horizontalCenter: parent.horizontalCenter text: "Save" onClicked: { console.log("image source: " + image.source); downloader.download(image.source) } } } }}
在这里,我们使用了DownloadManger里的SingleDownload来把我们所想要存的照片image存到我们想要存的地方。这个地方有上面的Dialog所提供。
为了使得这个应用能够正常运行,我们必须添加content_exchange及content_exchange_source到apparmor文件中去。否则我们将收到security的一些错误信息。
contenthub-savefile.apparmor
{ "policy_groups": [ "networking", "webview", "content_exchange", "content_exchange_source" ], "policy_version": 1.3}
我们从上面可以看出,我们把我们自己images目录下的sample.jpg传到我们的Gallery应用拥有的目录里了。
qml: download finished! saved path: /home/phablet/.cache/com.ubuntu.gallery/HubIncoming/12/sample.jpg
0 0
- 如何通过ContentHub把内容从一个地方传到另外一个地方
- 如何创建一个ContentHub exporter
- 利用Java代码实现把一个文件从一个地方剪切到另一个地方
- Java 练习把一个文件从一个地方剪切到另一个地方
- 如何把一个数组从客户端传到服务器端
- 查找某个数据,找到后把符合条件数据的一行复制到另外一个地方
- 使用iframe如何通过url传参把数据从一个页面传到另一个页面(contentWindow)
- 把选中的商品信息传到另外一个页面
- 如何使用asp.net中的控件将一个图片文件从一个目录传到另外一个目录下?
- aws 复制当前环境到另外一个地方
- c语言中如何把一个文件中的内容复制到另外一个文件中的代码
- 如何把一个DataTable中的某列通过表关系加到另外一个DataTable中
- 如何把一个DataTable中的某列通过表关系加到另外一个DataTable中
- SQL数据库,如何把一张表从一个数据库中插入到另外一个数据库?
- SQL数据库,如何把一张表从一个数据库中插入到另外一个数据库
- SQL数据库,如何把一张表从一个数据库中插入到另外一个数据库?
- 用js把数据从一个页面传到另一个页面
- 一个好地方
- Android采用ListView实现数据列表显示
- 系统默认只有一张内置的静态壁纸,如如何修改可以内置多张静态壁纸
- LeetCode(35) Search Insert Position
- Java 泛型
- Listview_OnItemClickListener失效的问题
- 如何通过ContentHub把内容从一个地方传到另外一个地方
- 虚拟键盘与fixed带给移动端的痛!
- HDOJ 5392 Infoplane in Tina Town LCM
- 无法保存打印机设置 0x000006d9
- 单词拆解&前缀树&树上DP LA 3942 Remember the Word
- UILabel使用自定义字体
- C++读写.mat文件
- Android 升级数据库的最佳写法
- 蚂蚁金服张洁:基于深度学习的支付宝人脸识别技术解秘