android - Google 云打印 WebView 示例不起作用

标签 android google-cloud-print

我正确设置了我的打印机,从第三方应用打印作品。

但我想从我自己的 android 应用程序打印。我贴了官方教程: https://developers.google.com/cloud-print/docs/android

但是 WebView 中的打印按钮不起作用。

有什么诀窍:)

最佳答案

我遇到了同样的问题,直到我在 Android 开发者网站上找到解决方案:http://developer.android.com/guide/webapps/webview.html .

您应用的 AndroidManifest.xml 中的 targetSdkVersion 可能设置为 17 或更高。在这种情况下,您需要对从 Google Developer 网站获得的 PrintDialogActivity 做一些小改动。您需要将注释 @JavascriptInterface 添加到 PrintDialogJavaScriptInterface 类中的公共(public)方法。

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();
        }
    }
}

关于android - Google 云打印 WebView 示例不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14773323/

相关文章:

java - 如何从 arrayList 中删除一项

oauth-2.0 - 谷歌云打印获取访问 token

node.js - 谷歌云打印 API : User credentials required

Android:字符串设置首选项不是持久的

android - 如何保持滚动条的速度?

android - 如何捕获运行时android中的未知异常

android - android中的PlaceAutoCompleteFragment在输入第一个字母后不显示建议并关闭

cordova - 谷歌云打印不适用于phonegap

javascript - 谷歌云打印 API key