0

我开发了一个使用 PrintHelper 或 Hp 移动打印 SDK 打印照片的 Android 应用程序。但是,在打印过程之前,屏幕上会出现 Android 打印对话框。

当应用程序打印照片时,如何跳过 android 打印对话框?

我已经遇到过以下问题。简而言之,答案是“没有办法做到这一点”。

但是,我尝试使用HP ePrint android 应用程序打印任何照片。在这个应用程序中,没有任何Android Print Dialog,它可以直接打印任何文档,而不会在屏幕上显示Android Print Dialog。

这意味着,有一种方法可以直接打印文档。

4

1 回答 1

1

通过调用打印适配器生命周期方法,可以将 printint 转换为 PDF。但是,由于回调是非公共抽象类,并且如果提供 null,系统会抛出段错误,因此您需要使用DexMaker来实现它们。我已经为 webView 适配器实现了这样的:

@Override
protected void onPreExecute() {
    super.onPreExecute();
    printAdapter = webView.createPrintDocumentAdapter();
}

@Override
protected Void doInBackground(Void... voids) {

    File file = new File(pdfPath);
    if (file.exists()) {
        file.delete();
    }
    try {
        file.createNewFile();

        // get file descriptor
        descriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);

        // create print attributes
        PrintAttributes attributes = new PrintAttributes.Builder()
                .setMediaSize(PrintAttributes.MediaSize.ISO_A4)
                .setResolution(new PrintAttributes.Resolution("id", PRINT_SERVICE, 300, 300))
                .setColorMode(PrintAttributes.COLOR_MODE_COLOR)
                .setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0))
                .build();
        ranges = new PageRange[]{new PageRange(1, numberPages)};

        // dexmaker cache folder
        cacheFolder =  new File(context.getFilesDir() +"/etemp/");

        printAdapter.onStart();

        printAdapter.onLayout(attributes, attributes, new CancellationSignal(), getLayoutResultCallback(new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {

                if (method.getName().equals("onLayoutFinished")) {
                    onLayoutSuccess();
                } else {
                    Log.e(TAG, "Layout failed");
                    pdfCallback.onPdfFailed();
                }
                return null;
            }
        }, cacheFolder), new Bundle());
    } catch (IOException e) {
        e.printStackTrace();
        Log.e(TAG, e != null ? e.getMessage() : "PrintPdfTask unknown error");
    }
    return null;
}

private void onLayoutSuccess() throws IOException {
    PrintDocumentAdapter.WriteResultCallback callback = getWriteResultCallback(new InvocationHandler() {
        @Override
        public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
            if (method.getName().equals("onWriteFinished")) {
                pdfCallback.onPdfCreated();
            } else {
                Log.e(TAG, "Layout failed");
                pdfCallback.onPdfFailed();
            }
            return null;
        }
    }, cacheFolder);
    printAdapter.onWrite(ranges, descriptor, new CancellationSignal(), callback);
}


/**
 * Implementation of non public abstract class LayoutResultCallback obtained via DexMaker
 * @param invocationHandler
 * @param dexCacheDir
 * @return LayoutResultCallback
 * @throws IOException
 */
public static PrintDocumentAdapter.LayoutResultCallback getLayoutResultCallback(InvocationHandler invocationHandler,
                                                                                File dexCacheDir) throws IOException {
    return ProxyBuilder.forClass(PrintDocumentAdapter.LayoutResultCallback.class)
            .dexCache(dexCacheDir)
            .handler(invocationHandler)
            .build();
}

/**
 * Implementation of non public abstract class WriteResultCallback obtained via DexMaker
 * @param invocationHandler
 * @param dexCacheDir
 * @return LayoutResultCallback
 * @throws IOException
 */
public static PrintDocumentAdapter.WriteResultCallback getWriteResultCallback(InvocationHandler invocationHandler,
                                                                              File dexCacheDir) throws IOException {
    return ProxyBuilder.forClass(PrintDocumentAdapter.WriteResultCallback.class)
            .dexCache(dexCacheDir)
            .handler(invocationHandler)
            .build();
}
于 2016-10-20T07:14:51.077 回答