使用ContentHub来导入我们需要的照片
2015-08-06 16:37
429 查看
我们知道,在Ubuntu 手机平台中,我们使用了全新的安全机制。任何一个应用只能访问自己的私有空间,但是它不能访问不属于它的任何其它的空间。具体来说,如果我们想直接通过系统文件目录的方式来使用照相机所拥有的Pictures目录的话,那是不可以的。原因是那个目录只属于创建它的那个应用。那么我们该如何才能访问那个目录中的文件呢?答案就是ContentHub
API。我们在先前的一个例子“利用ContentHub API来import图片”中也展示了如何这么做。
在桌面系统上,我们必须安装如下的包:
下面我们来通过一个例子来展示如何来导入一个从其它应用产生的图片:
在上面的代码中,我们使用了ContentPeerPicker来进行选择我们所需要的图片的Peer。当我们一旦选择好Peer后,我们通过
来得到目前activeTransfer的具体的状态。当它的状态变为ContentTransfer.Charged时,我们就可以得到所选文件的url。下面是我们运行我们的应用时的截图。记住我们需要添加content_exchange policy来完成我们的操作:
在图片被import过来后,我们会发现照片所处的位置在:
显然这个位置是我们的应用可以访问的位置。我们可以对它进行我们所需要的任何操作。
整个项目的源码在: https://github.com/liu-xiao-guo/contenthub-picker
API。我们在先前的一个例子“利用ContentHub API来import图片”中也展示了如何这么做。
在桌面系统上,我们必须安装如下的包:
$ sudo apt-get install content-hub $ sudo apt-get install qtdeclarative5-ubuntu-content1
下面我们来通过一个例子来展示如何来导入一个从其它应用产生的图片:
Main.qml
import QtQuick 2.4 import Ubuntu.Components 1.2 import Ubuntu.Components.ListItems 0.1 as ListItem import Ubuntu.Components.Popups 0.1 import Ubuntu.Content 0.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-picker.liu-xiao-guo" /* This property enables the application to change orientation when the device is rotated. The default is false. */ //automaticOrientation: true width: units.gu(60) height: units.gu(85) PageStack { id: pageStack Component.onCompleted: pageStack.push(root) Page { id: root title: i18n.tr("Peer Picker Example") visible: false property list<ContentItem> importItems property var activeTransfer Column { anchors.fill: parent ListItem.Standard { id: peerListHeader anchors { left: parent.left right: parent.right } text: i18n.tr("Sources") control: Button { text: i18n.tr("Select source") onClicked: { pageStack.push(picker); } } } ListItem.Header { id: titleItem anchors { left: parent.left right: parent.right } text: i18n.tr("Results") } GridView { id: resultList anchors { left: parent.left right: parent.right } height: childrenRect.height cellWidth: units.gu(20) cellHeight: cellWidth model: root.importItems delegate: Item { id: result height: units.gu(19) width: height UbuntuShape { width: parent.width height: width source: Image { id: image source: url sourceSize.width: width sourceSize.height: height height: parent.height width: height fillMode: Image.PreserveAspectFit smooth: true } } } } } ContentTransferHint { anchors.fill: root activeTransfer: root.activeTransfer } Connections { target: root.activeTransfer onStateChanged: { console.log("StateChanged: " + root.activeTransfer.state); switch (root.activeTransfer.state ) { case ContentTransfer.Created: console.log("StateChanged: Created") break; case ContentTransfer.Initiated: console.log("StateChanged: Initiated") break; case ContentTransfer.InProgress: console.log("StateChanged: InProgress") break; case ContentTransfer.Downloading: console.log("StateChanged: Downloading") break; case ContentTransfer.Downloaded: console.log("StateChanged: Downloaded") break; case ContentTransfer.Charged: console.log("StateChanged: Charged") break; case ContentTransfer.Collected: console.log("StateChanged: Collected") break; case ContentTransfer.Aborted: console.log("StateChanged: Aborted") break; case ContentTransfer.Finalized: console.log("StateChanged: Finalized") break; } if (root.activeTransfer.state === ContentTransfer.Charged) { root.importItems = root.activeTransfer.items; for ( var key in root.importItems ) { console.log("url: " + root.importItems[key].url) } } } } } Page { id: picker visible: false ContentPeerPicker { visible: parent.visible // Type of handler: Source, Destination, or Share handler: ContentHandler.Source // well know content type contentType: ContentType.Pictures onPeerSelected: { console.log("onPeerSelected-----------------------------!") root.activeTransfer = peer.request(); pageStack.pop(); } onCancelPressed: { console.log("onCancelPressed!---------------------------!") pageStack.pop(); } } } } }
在上面的代码中,我们使用了ContentPeerPicker来进行选择我们所需要的图片的Peer。当我们一旦选择好Peer后,我们通过
Connections { target: root.activeTransfer onStateChanged: { ... } }
来得到目前activeTransfer的具体的状态。当它的状态变为ContentTransfer.Charged时,我们就可以得到所选文件的url。下面是我们运行我们的应用时的截图。记住我们需要添加content_exchange policy来完成我们的操作:
在图片被import过来后,我们会发现照片所处的位置在:
qml: url: file:///home/phablet/.cache/contenthub-picker.liu-xiao-guo/HubIncoming/3/image20150806_080715636.jpg
显然这个位置是我们的应用可以访问的位置。我们可以对它进行我们所需要的任何操作。
整个项目的源码在: https://github.com/liu-xiao-guo/contenthub-picker
相关文章推荐
- Protocol - 3
- RHEL 6 和 RHEL 7 之间的变化(部分内容)
- 循环报数
- offsetLeft和offsetTop的定位机制
- hdu 1166 敌兵布阵 (树状数组)
- 增加 addDataScheme("file") 才能收到SD卡插拔事件的原因分析 -- 浅析android事件过滤策略
- js跳转页面方法
- Android之Wifi模块开发
- 病毒HEUR:Trojan-Downloader.Win32.Generic
- jquery ajax验证注册
- 网站权重提高技巧
- A Contesting Decision
- LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal
- c#外部调用类里边的函数
- Tomcat内存溢出的三种情况及解决办法分析
- 关于两个标签的比较@queryparam和@pathparam
- thinkphp验证码
- 关于iexplore.exe占用CPU100%的问题
- Cocos2d-x 3.6 项目实战---贪吃蛇(3)
- subprocess.Popen()详解