【问题标题】:Google Cloud Print WebView example doesn't workGoogle 云打印 WebView 示例不起作用
【发布时间】:2013-07-02 02:30:37
【问题描述】:

我的打印机设置正确,可以通过第三方应用打印。

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

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

有什么诀窍:)

【问题讨论】:

    标签: android google-cloud-print


    【解决方案1】:

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

    您应用的 AndroidManifest.xml 中的 targetSdkVersion 可能设置为 17 或更高。在这种情况下,您需要对从 Google Developer 网站获得的 PrintDialogActivity 进行小幅更改。您需要在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();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-23
      • 2012-02-15
      • 2012-04-07
      • 1970-01-01
      • 2012-01-26
      • 2015-08-14
      • 2011-12-14
      • 2012-04-12
      相关资源
      最近更新 更多