Launching Activities and Using Content URIs in Android

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:

You can see in the code that Marshall uses an Intent to tell Android what action he wants to perform (“com.google.zxing.client.android.SCAN”) 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:

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

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://)

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.

[…] to record audio without using Intents. Don’t get me wrong, the post published by Bill Dawson (Launching Activities and Using Content URIs in Android), was awesome and it really helped a lot, but we all needed a way to do this without using an […]

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.

Follow Us

What can we help you with today?

Let us know how we can help you today! We're ready to get down to business. To contact our sales click here or fill out the form below: Need support? Log in to our support portal for assistance.

Axway respects your privacy. Your personal data will not be shared with or sold to a third party (unless to Axway legal entities where you can find the list at "Contact us"). Please note that you can withdraw your consent at any time by clicking here.