Using the Contenthub API to import images

Source: Internet
Author: User

Because of the security of Ubuntu OS, no third-party apps can access the storage of another application. This poses a problem, such as how do we want to access the images in gallery? We can use the Contenthub API provided by Ubuntu OS to access the accessible content provided by another application. The premise is that another application must have the corresponding function to realize the function of exporter. In this article, we'll show you how to use Contenthub to get the pictures provided by gallery. For more information about Contenthub's API, you can refer to the link.


1) Create a basic QML application we open the SDK to create a basic QML application. We use the "App with simple UI" template. And take a name of "Contenthub-importer". Here, we must pay attention to the following items:
ApplicationName: "Com.ubuntu.developer.liu-xiao-guo.contenthub-importer"

The name of the app must be the same as the package name. Otherwise, when we import pictures into this app, the directories will not be accessible because of the different paths to the directory. The package name we will generate is:
Com.ubuntu.developer.liu-xiao-guo.contenthub-importer_0.1_all.click

The path to import on the phone will be:


This is, of course, the path we can create when we finally run the app, after importing the picture.

2) Design our interface with our design interface as follows:


Above the screen, it is a "Combobutton". Our design is as follows:
        Listmodel {id:typemodel listelement {name: "Import single Item"} Listelement        {Name: "Import multiple Items"}}                    Listitem.empty {id:options Combobutton {id:type Anchors {                Left:parent.left Margins:units.gu (2)} Dropdowncolor: "Red"                WIDTH:ROOT.WIDTH/2 expanded:false Text: "Import single item" ListView {anchors.fill:parent Model:typemodel delegate:listi Tem. Standard {Text:modeldata onclicked: {CONSOLE.L                            OG ("Item is clicked!" + index + "+ name");                            type.expanded = false;                            Type.text = text;          Console.log ("type is:" + type.text);                  if (name = = = "Import Single item") {Activetransfer = Picsourcesingle.re                            Quest (AppStore); } else if (name = = = "Import multiple items") {Activetransfer = Picsourcemulti.request (AP                            PStore);                     }}}}} Button {anchors { Right:parent.right Margins:units.gu (2)} text: "Fin Alize Import "enabled:activeTransfer.state = = = Contenttransfer.collected Onclicked:activet Ransfer.finalize ()}}

We can choose to import one or more pictures. We also define two contentpeer. A contentpeer is a destination that has been registered with Contenthub as a data source or a contenttype definition.
        Contentpeer {            id:picsourcesingle            contentType:ContentType.Pictures            Handler:ContentHandler.Source            SelectionType:ContentTransfer.Single        }        Contentpeer {            id:picsourcemulti            contentType:ContentType.Pictures            Handler:ContentHandler.Source            SelectionType:ContentTransfer.Multiple        }

We can also define a "Contentpeermodel" to show all applications that can provide images.
        Provides a list<contentpeer> suitable for use as a model        Contentpeermodel {            id:picsources            //Type of Handler:source, Destination, or Share            handler:ContentHandler.Source            //Well know content type            ContentType : Contenttype.pictures        }

We can use the following ListView to display these applications:
        ListView {            id:peerlist            anchors {                left:parent.left                right:parent.right                top:options.bottom            }            height:childrenRect.height            model:picSources.peers            delegate:ListItem.Standard {                text: Modeldata.name                Control:button {                    text: "Import"                    onclicked: {                        //Request the transfer, it needs to be C Reated and dispatched from the hub                        Activetransfer = Modeldata.request ();}}}        

We can use the following methods to detect the state in the transfer process:
        Connections {Target:activetransfer onstatechanged: {//console.                Log ("statechanged:" + activetransfer.state);                    Switch (activetransfer.state) {case ContentTransfer.Created:console.log ("Created");                    Break Case ContentTransfer.Initiated:console.log ("initiated");                Break                    Case ContentTransfer.InProgress:console.log ("InProgress");                Break                    Case ContentTransfer.Downloading:console.log ("downloading");                Break                    Case ContentTransfer.Downloaded:console.log ("downloaded");                Break                    Case ContentTransfer.Charged:console.log ("Charged");                Break Case ContentTransfer.Collected:console.log ("Collected");                Break                    Case ContentTransfer.Aborted:console.log ("aborted");                Break                    Case ContentTransfer.Finalized:console.log ("finalized");                Break                    Default:console.log ("not recognized state!")                Break } if (activetransfer.state = = = contenttransfer.charged) {Importitems = Activetransfer.i                    TEMs                    for (var i = 0; i < importitems.length; i + +) {Console.log (Importitems[i].url);                    } var Item;                    For (item in Importitems) {console.log ("Imported URL:" + importitems[item].url); }                }            }

In the code above, we show the path to the files stored in this app. Here Activetransfer and importitems are defined as follows:
    Property List<contentitem> Importitems Property    var activetransfer

We use a different ListView to display the photos we have imported:
        ListView {            id:peerlist            anchors {                left:parent.left                right:parent.right                top:options.bottom            }            height:childrenRect.height            model:picSources.peers            delegate:ListItem.Standard {                text: Modeldata.name                Control:button {                    text: "Import"                    onclicked: {                        //Request the transfer, it needs to be C Reated and dispatched from the hub                        Activetransfer = Modeldata.request ();}}}        

The most applied display results are as follows:


The entire code can be found at the following address:
BZR BranchLp:~liu-xiao-guo/debiantrial/contenthub-importer



Using the Contenthub API to import images

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.