Courtesy of Andrew D

We enjoy a challenge in the Signals studio and during a recent AIR based project for the Android based Motorola Xoom we found one:
The tablet app needed to open files with their default application based on their file type/extension.
Sounds easy? Well if you're using Adobe AIR in the desktop environment it is and can be achieved using code similar to the following:
var file:File = File.desktopDirectory.resolvePath("a.pdf");
file.openWithDefaultApplication();
However, during development we soon discovered that Adobe AIR for Android does not support the openWithDefaultApplication function. Running this code on the Android device simply returned errors or did nothing at all. Furthermore we found the device did not support pdf viewing in either StageWebView or the browser. Attempting to use a combination of URLLoader, URLRequest and the navigateToURL function also proved ineffective.
In fact, the only way to view a pdf on the device was through a third party browser with a pdf viewer plug-in installed and loading the pdf from a hosted server, not a local version stored on the device. As we also needed support for other file types, such as Office documents and videos, we needed to find an alternative approach.
After a lot more research and testing we came across an article that gave us a solution and explains the issue in detail. This resulted in us using the following Java to interpret the AIR commands:
//Example for pdf open with default application
File file = new File(url);
if (file.exists())
{
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
try
{
getApplicationContext().startActivity(intent);
}
catch (ActivityNotFoundException e)
{
//Decide what to do if there is no application that handle the PDF MIME.
}
}
Once our Flash Builder compiled AIR swf within the Java wrapper we were able to achieve the functionality we needed in our application.
Select the CODE tag to see more of our free code blog posts.
Interested in Smartphone and tablet apps?