Send 2 Printer tries to hook into existing applications to enable printing but you can also send the following data formats to Send 2 Printer for printing:
- HTML content
- Text
- Image
- Canvas draw commands (via serialized Picture)
- Canvas draw commands rendered to a Bitmap
Below is a link to a sample Android eclipse project that prints sample content from all of the above formats.
Test Print Application
For example, to print canvas draw commands:
- Code: Select all
void printCanvasExample()
{
// create canvas to render on
Picture picture = new Picture();
Canvas c = picture.beginRecording( 240, 240 );
// fill background with WHITE
c.drawRGB( 0xFF, 0xFF, 0xFF );
// draw text
Paint p = new Paint();
Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
p.setTextSize( 18 );
p.setTypeface( font );
p.setAntiAlias(true);
Rect textBounds = new Rect();
p.getTextBounds( HELLO_WORLD, 0, HELLO_WORLD.length(), textBounds );
int x = (c.getWidth() - (textBounds.right-textBounds.left)) / 2;
int y = (c.getHeight() - (textBounds.bottom-textBounds.top)) / 2;
c.drawText( HELLO_WORLD, x, y, p );
// draw icon
Bitmap icon = BitmapFactory.decodeResource( getResources(), R.drawable.icon );
c.drawBitmap( icon, 0, 0, null );
// stop drawing
picture.endRecording();
// queue canvas for printing
File f = PrintUtils.saveCanvasPictureToTempFile( picture );
if( f != null )
{
PrintUtils.queuePictureStreamForPrinting( this, f );
}
}
To print a bitmap or a canvas rendered bitmap:
- Code: Select all
void printCanvasAsBitmapExample()
{
// create canvas to render on
Bitmap b = Bitmap.createBitmap( 240, 240, Bitmap.Config.RGB_565 );
Canvas c = new Canvas( b );
// fill background with WHITE
c.drawRGB( 0xFF, 0xFF, 0xFF );
// draw text
Paint p = new Paint();
Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
p.setTextSize( 18 );
p.setTypeface( font );
p.setAntiAlias(true);
Rect textBounds = new Rect();
p.getTextBounds( HELLO_WORLD, 0, HELLO_WORLD.length(), textBounds );
int x = (c.getWidth() - (textBounds.right-textBounds.left)) / 2;
int y = (c.getHeight() - (textBounds.bottom-textBounds.top)) / 2;
c.drawText( HELLO_WORLD, x, y, p );
// draw icon
Bitmap icon = BitmapFactory.decodeResource( getResources(), R.drawable.icon );
c.drawBitmap( icon, 0, 0, null );
// queue bitmap for printing
try
{
File f = PrintUtils.saveBitmapToTempFile( b, Bitmap.CompressFormat.PNG );
if( f != null )
{
PrintUtils.queueBitmapForPrinting( this, f, Bitmap.CompressFormat.PNG );
}
}
catch( Exception e )
{
Log.e( TAG, "failed to save/queue bitmap", e );
}
}
The above results the following output:

To print text or a text file:
- Code: Select all
void printTextExample()
{
try
{
File f = PrintUtils.saveTextToTempFile( HELLO_WORLD );
if( f != null )
{
PrintUtils.queueTextFileForPrinting( this, f );
}
}
catch( Exception e )
{
Log.e( TAG, "failed to save/queue text", e );
}
}
To print HTML content or a HTML file:
- Code: Select all
void printHtmlExample()
{
try
{
StringBuilder buf = new StringBuilder();
buf.append( "<html>" );
buf.append( "<body>" );
buf.append( "<h1>" ).append( HELLO_WORLD ).append( "</h1>" );
buf.append( "<p>" ).append( "blah blah blah..." ).append( "</p>" );
buf.append( "<img src=\"http://www.google.com/intl/en_ALL/images/logo.gif\" />" );
buf.append( "</body>" );
buf.append( "</html>" );
File f = PrintUtils.saveHtmlToTempFile( buf.toString() );
if( f != null )
{
PrintUtils.queueHtmlFileForPrinting( this, f );
}
}
catch( Exception e )
{
Log.e( TAG, "failed to save/queue html", e );
}
}
The above results the following output:

You'll need to install Send 2 Printer from the Android Market in order to print. For a limited time, you can also manually install it below for testing:
Send 2 Printer
NOTE: (API works with version installed from Market and not the ADC2 Challenge version due to different package names)
