Appcelerator Blog

The Leading Resource for All Things Mobile

Launching Activities and Using Content URIs in Android

0 Flares 0 Flares ×

Titanium Mobile 1.5.0, released in December 2010, brought with it several enhancements which extended the possibilities for your Titanium Android application to interact with other Android applications, whether or not those other applications are built with Titanium. Marshall Culpepper and Kevin Whinnery introduced you to a lot of that new functionality in a Titanium webinar in early January 2010. If you missed that, or if you’re just now starting to get interested in how to use Titanium to tap into native Android functionality, I highly recommend that you watch the video. Also study our API documentation for the Titanium.Android namespace.

In the webinar video, starting at about 26:00, Marshall shows a series of examples of opening external applications by starting their associated Activities. (He also does a great job of explaining Android terms such as “Activity” and “Intent” beginning at about 3:10, so if you don’t understand my use of the term “Activities”, then that’s another good reason to check out the video.) I’m not going to re-hash everything here, but as a quick reminder, here’s an excerpt of the part of Marshall’s test application which launches an activity associated with a third-party barcode image processing library called ZXing:

[gist id=839101]

You can see in the code that Marshall uses an Intent to tell Android what action he wants to perform (“”) and to pass a string (the desired “SCAN_MODE”, which is “QR_SCAN_MODE”) to the Activity which will be launched to perform that action. He then passes that Intent to startActivityForResult, along with a function that will be called-back when the Activity finishes.

So in that example, Marshall only needed to put a string value into that Intent, and we supported that just fine in 1.5.0. But shortly after Titanium 1.5.0 and its quick successor, 1.5.1, were released, we — and several of you — realized that we were missing a key piece of functionality that prevented developers from taking advantage of Intents and startActivityForResult for a fairly common use-case: launching an Activity that does something with a “content URI“. An Android content URI is a URI generated by one of the many “content providers” on your Android device. There are content providers that manage your contacts as well as sound, video and image data. Each URI points to a specific “record” or set of records, a record being a database table entry which points to a piece of data such as an individual contact or a photo.

Our problem was that the Activities which make use of these content URIs expect the URIs to be passed to them in code as instances of Android’s Uri class, and not simply as their string representations (such as “content://contacts/people/3”). We had overlooked the need to provide a way for you to put true content URIs into the Intents that are passed to these Activities. We have remedied that in Titanium Mobile 1.6.0 with the new putExtraUri method of Titanium.Android.Intent, which lets you pass the string version of the URI but then converts it to a true Uri instance for you.  Now you can finally take advantage of the parts of Android native functionality which use content providers and therefore require content URIs.

We’ll leave you with a Titanium example that uses the new putExtraUri method.  This little app (it can be used as your complete app.js) makes a voice recording and shares it.  In order to do the sharing, it needs to give the Intent the URI to the recording, so it calls putExtraUri:

[gist id=839453]

Enjoy finding new ways to use Titanium to work with the many content providers available on Android devices.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×


  1. Marc

    Nice article, I have a question though: if I wanted to get the recording as a blob (so I could post it to a web service, for example) – how would I do that? Using getFile on the provided URI does not work (even if I replace content:// with file://)

    Any ideas?


  2. Brian

    I have the same question as Marc – we need to get the file to transfer back to our webserver –

    How do you determine what the recording file name is and get it?


  3. A content URI requires querying the content provider and asking for data to be returned. It’s very similar to database access. We haven’t exposed querying content providers yet. The quickest route would be for you to put a quick module together to get the file URL from the content provider.

    You can see in our [MediaModule]( where we set up a projection to get information out about the content referred to in the content URI.

  4. The returned ‘soundUri’ from isn’t really very useful, from what I can tell (so far).

    It gives something like : content://media/external/audio/media/13 (for example)

    I can see from the debug that there’s a propertiy called _data which does actually contain the real path to the recorded file. This would be much more useful to use.

    I tried doing a ‘getFile’ on the returned soundUri and get nothing. Also the soundUri does not seem to have any bearing or relation to where the file has actually been stored. ?? I take it this is just a media database entry?

    It would be very helpful if we could get to the full set of data which the SoundRecorder generates after a successful recording.


    D/SoundRecorder( 707): Inserting audio record: title=2011-08-17 13:39:44 mime_type=audio/3gpp date_modified=1313588383 date_added=1313588384 album=Audio recordings artist=Your recordings is_music=0 _data=/mnt/sdcard/recording-1566381020.3gpp

  5. Arturo Bruno

    Could this work on iOS?

Comments are closed.

Sign up for updates!

Become a mobile leader. Take the first step to scale mobile innovation throughout your enterprise.
Get in touch
computer and tablet showing Appcelerator software
Start free, grow from there.
0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×