如何使用wifi打印机打印我的布局?

问题描述 投票:1回答:1

我正在开发餐厅应用程序。订单完成后,我需要在AlertView中显示账单(商品价格和总金额)。

是否可以在wifi打印机中从AlertView中取出打印内容?如何制作?

android android-layout android-wifi android-print-framework
1个回答
1
投票

是的,可以打印布局,从布局创建位图,然后将意图发送到打印机应用程序,如以下代码所示

    yourRootLayout.setDrawingCacheEnabled(true);
    yourRootLayout.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    yourRootLayout.buildDrawingCache();

    Bitmap dummyImageBitmap = Bitmap.createBitmap(yourRootLayout
            .getDrawingCache());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    dummyImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
    byte[] photoArray = out.toByteArray();
    yourRootLayout.setDrawingCacheEnabled(false);

    try {

        File file = new File(getFilePath());//path to sdcard
        if (file.exists()) {
            file.delete();
        }
        FileOutputStream outPut = new FileOutputStream(file);
        outPut.write(photoArray);
        outPut.flush();
        outPut.close();
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("application/pdf");
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        startActivity(intent);


    } catch (Exception e) {
        throw new Exception("Error generating file", e);
    }

现在这将弹出一个应用程序列表,您必须选择允许通过wifi打印的打印机应用程序

© www.soinside.com 2019 - 2024. All rights reserved.