我正确设置了我的打印机,从第三方应用打印作品。
但我想从我自己的 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/