您的位置:首页 > 其它

使用ContentHub来导入我们需要的照片

2015-08-06 16:37 429 查看
我们知道,在Ubuntu 手机平台中,我们使用了全新的安全机制。任何一个应用只能访问自己的私有空间,但是它不能访问不属于它的任何其它的空间。具体来说,如果我们想直接通过系统文件目录的方式来使用照相机所拥有的Pictures目录的话,那是不可以的。原因是那个目录只属于创建它的那个应用。那么我们该如何才能访问那个目录中的文件呢?答案就是ContentHub
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: