Photo Printing on Android


I always find it interesting to watch the 4x100m Sprint Relay at the Olympics and Track and Field Championships. It’s the fastest and most technically difficult of all the relays. Demonstrating true synergy, a well executed relay transports the baton around the track faster than the sum of the individual personal best 100m times.

Synergy is usually defined as the interaction or cooperation of two or more agents to produce a combined effect, greater than the sum of their separate effects. Aristotle of course was more concise; “The whole is more than the sum of its parts.”
When several applications work closely together, synergism can also be observed on Android. Android introduced the powerful yet simple concept of Intents, a facility to perform late runtime binding inside and even between applications.

Android applications expose their Activities and related Intents-Filters through the AndroidManifest.xml, and Activities implicitly or explicitly set to exportable can be used by other applications. HP iPrintPhoto for instance invites other applications to use its PRINT Action, by declaring the following Activity in its AndroidManifest:

<activity android:name=".photo.PrintPhotoActivity" android:exported="true" android:enabled="true">
<intent-filter>
<action android:name="org.androidprinting.intent.action.PRINT"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*"/>
</intent-filter>
..
</activity>

Application that like to use the image printing feature provided by iPrint Photo, would create and new Intent and start the print Activity, like so:

Intent intent = new Intent("org.androidprinting.intent.action.PRINT")
.addCategory(Intent.CATEGORY_DEFAULT)
.setDataAndType(uri,"image/*");
startActivity( intent );

The example above assumed that an Uri to an image already existed. If that’s not the case, and for instance the content of a Canvas needed to be printed, then the Uri could easily be created on the fly:

Bitmap mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
Canvas mCanvas = new Canvas(mBitmap);
String filename = String.valueOf(System.currentTimeMillis());
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, filename);
values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
Uri uri= getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
 
OutputStream outStream = getContentResolver().openOutputStream(uri);
bmp.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
outStream.flush();
outStream.close();
 
Intent intent = new Intent("org.androidprinting.intent.action.PRINT")
.addCategory(Intent.CATEGORY_DEFAULT)
.setDataAndType(uri,"image/*");
startActivity( intent );

While all Android applications expose at least their launch Activity, some go a step further, and by marking Activities as exportable, allowing them to be called from other applications. HP iPrint Photo has gone the extra mile. Not only is its print Activity exported, but by explicitly choosing a vendor-neutral name for the Print Action “org.androidprinting.intent.action.PRINT”, the door is kept wide open for other printer vendors to join-in, using the same name to allow printing to their printers as well, which in turn would make printing applications vendor independent.
A good place to learn about available Activities and how to correctly create the Intents to start them, is the OpenIntents registry.
Besides enabling synergism on the application level, Android demonstrates mobility, skill, and more recently an almost explosive speed, which are the predominant requirements for a successful decathlete. The decathlon is another interesting highlight at Track and Field Championships, I love to watch. It’s a supreme test, whose champion may claim the title “world’s greatest athlete.”

 

 

Leave a Reply