在android上使用配置的打印机打印图像

时间:2012-01-04 04:10:46

标签: android printing

我使用以下代码将命令发送到打印机。这里的问题是它应该拍摄图像并将文件发送到tempfolder。该文件应该发送到打印机并打印。但是没有创建临时文件夹,也没有创建文件。你能帮帮忙吗?

void printCanvasExample()     {         //创建要渲染的画布         图片图片=新图片();         画布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 );
    }
    else
    {

        new AlertDialog.Builder(this).setTitle("Argh").setMessage("Watch out!").setNeutralButton("Close", null).show();  

    }
}

在PrintUtils.java中创建临时文件夹的代码是 public static文件saveCanvasPictureToTempFile(图片图片)     {         文件tempFile = null;

    // save to temporary file
    File dir = getTempDir();
    if( dir != null )
    {
        FileOutputStream fos = null;
        try
        {
            File f = File.createTempFile( "picture", ".stream", dir );
            fos = new FileOutputStream( f );
            picture.writeToStream( fos );
            tempFile = f;
        }
        catch( IOException e )
        {
            Log.e( TAG, "failed to save picture", e );
        }
        finally
        {
            close( fos );
        }
    }       

    return tempFile;
}

有关失踪的建议吗?

0 个答案:

没有答案