无法从设备打印

时间:2014-12-22 11:24:17

标签: android google-cloud-print

我已经跟随tutorial on the cloud print website并通过复制和粘贴示例代码创建了一个Print活动。

我试图从MediaStore打印图像,但是当我按下打印屏幕后,在按下“打印”后没有任何反应。按钮。

这是我用来调用意图

的代码
Intent printIntent = new Intent(GalleryActivity.this, PrintDialogActivity.class);

Uri fileUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Long.toString(imageId));
Log.d(this, "File Uri:" + fileUri);
printIntent.setDataAndType(fileUri, "image/*");
startActivity(printIntent);

记录的Uri看起来像内容:// media / external / images / media / 26848

按下打印按钮时的Logcat输出是

[INFO:CONSOLE(1)] "Uncaught TypeError: Object [object Object] has no method 'getType'", source: https://www.google.com/cloudprint/dialog.html (1)
[INFO:CONSOLE(280)] "Uncaught TypeError: Cannot call method 'k' of null", source: https://www.google.com/cloudprint/client/442365700-dialog_mobile.js (280)

编辑:我已经在其他几个设备上进行了测试,但我没有获得上述日志输出,因此可能没有相关性。但是,每个设备的结果都是一样的;当我在webview中按下打印按钮时没有任何反应。

1 个答案:

答案 0 :(得分:2)

在PrintDialogJavaScriptInterface类的方法中添加@JavascriptInterface。

final class PrintDialogJavaScriptInterface {

    @JavascriptInterface
    public String getType() {
        return cloudPrintIntent.getType();
    }

    @JavascriptInterface
    public String getTitle() {
        return cloudPrintIntent.getExtras().getString("title");
    }

    @JavascriptInterface
    public String getContent() {
        try {
            ContentResolver contentResolver = getContentResolver();
            InputStream is = contentResolver.openInputStream(cloudPrintIntent.getData());
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            byte[] buffer = new byte[4096];
            int n = is.read(buffer);
            while (n >= 0) {
                baos.write(buffer, 0, n);
                n = is.read(buffer);
            }
            is.close();
            baos.flush();

            return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    @JavascriptInterface
    public String getEncoding() {
        return CONTENT_TRANSFER_ENCODING;
    }

    @JavascriptInterface
    public void onPostMessage(String message) {
        if (message.startsWith(CLOSE_POST_MESSAGE_NAME)) {
            finish();
        }
    }
}