Android,向打印机发送多个文件

时间:2014-09-18 05:42:18

标签: android android-intent printing

我试着通过三星和其他应用程序打印,对于三星我使用下面的代码发送我的照片进行打印:

    Intent intent = new Intent(
                    "com.sec.print.mobileprint.action.PRINT");
    String rootSDCard = Environment.getExternalStorageDirectory()
                    .getAbsolutePath();
    Uri uri = Uri.parse(rootSDCard + "/Ali_mola/" + fname);
    intent.putExtra("com.sec.print.mobileprint.extra.CONTENT", uri);
    intent.putExtra("com.sec.print.mobileprint.extra.CONTENT_TYPE",
                    "PHOTO");
    intent.putExtra("com.sec.print.mobileprint.extra.OPTION_TYPE",
                    "DOCUMENT_PRINT");
    intent.putExtra("com.sec.print.mobileprint.extra.JOB_NAME",
                    "Untitled");
            startActivity(intent);

对于其他打印机我使用IntentACTION_SEND的正常意图,但我的问题是如何putExtra多个文件?

我使用了Uri[2],但它没有用。

1 个答案:

答案 0 :(得分:0)

我认为java可以完美地完成这项工作.. 在你的android应用程序代码中嵌入java代码。希望它能奏效。

           // get list of files and check directory

            String[] files = null;
            File inputFiles;

            /** make sure name ends with a deliminator for correct path later */
            if (!file_name.endsWith(separator)) {
                file_name = file_name + separator;
            }

            try {
                inputFiles = new File(file_name);

                if (!inputFiles.isDirectory()) {
                    System.err.println(file_name + " is not a directory. Exiting program");
                } else {
                    files = inputFiles.list();
                }
            }
            catch (Exception ee) {
                LogWriter.writeLog("Exception trying to access file " + ee.getMessage());
            }

            /** now work through all pdf files */
            for (String file : files) {

                if (file.toLowerCase().endsWith(".pdf")) {
                    logMessage(file_name + file);

                    decodeAndPrintFile(file_name + file);

                    // reset printer in case of lots of pages (cannot be reused)
                    validatePrinter(printerName);

                }
            }
        }

这是解码和打印代码。

     private void decodeAndPrintFile(String file_name) {

    /**
     * open the file and get page count
     */
    try {

        logMessage("Opening file :" + file_name + " to print.");

        pdfDecoder = new PdfDecoder(true);
        // decode_pdf.setExtractionMode(0, 72,1);
        pdfDecoder.openPdfFile(file_name);

    } catch (Exception e) {
        reportError("Exception " + e + " in pdf code");
    }

    /**
     * code to handle passwords - some PDFs have an empty password set
     */
    if (pdfDecoder.isEncrypted() && !pdfDecoder.isFileViewable()) {

        String password = null; // set your password here

        try {
            /** try and reopen with new password */
            if (password == null) {
                pdfDecoder.setEncryptionPassword(password);
            } else {
                pdfDecoder.setEncryptionPassword("");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * print if allowed and values found
     */
    if (pdfDecoder.isEncrypted() && !pdfDecoder.isFileViewable()) {
        logMessage("Encrypted settings");
    } else {

        // mappings for non-embedded fonts to use
        FontMappings.setFontReplacements();

        printPages();
    }

    /** close the pdf file */
    pdfDecoder.closePdfFile();

}

作为参考,您可以学习example