문제

I setup correctly my printer, printing works from third party app.

But I want to print from my own android application. I tied the official tutorial: https://developers.google.com/cloud-print/docs/android

But the Print button in WebView doesn't work.

What is the trick :)

도움이 되었습니까?

해결책

I had this same problem until I found the solution on the Android developer website: http://developer.android.com/guide/webapps/webview.html.

The targetSdkVersion in your app's AndroidManifest.xml is probably set to 17 or higher. In that case, you need to make a small change to the PrintDialogActivity that you got from the Google Developer website. You need to add the annotation, @JavascriptInterface to the public methods in the PrintDialogJavaScriptInterface class.

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();
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top