1
我正確安裝了我的打印機,從第三方應用程序打印作品。Google雲打印WebView示例不起作用
但我想從我自己的Android應用程序打印。我綁官方教程: https://developers.google.com/cloud-print/docs/android
但WebView中的打印按鈕不起作用。
什麼是我有同樣的問題,直到我發現了Android開發者網站上的解決方案的伎倆:)
我正確安裝了我的打印機,從第三方應用程序打印作品。Google雲打印WebView示例不起作用
但我想從我自己的Android應用程序打印。我綁官方教程: https://developers.google.com/cloud-print/docs/android
但WebView中的打印按鈕不起作用。
什麼是我有同樣的問題,直到我發現了Android開發者網站上的解決方案的伎倆:)
:http://developer.android.com/guide/webapps/webview.html。
您應用的AndroidManifest.xml中的targetSdkVersion
可能設置爲17或更高。在這種情況下,您需要對從Google開發者網站獲得的PrintDialogActivity
進行一些小改動。您需要將註釋@JavascriptInterface
添加到PrintDialogJavaScriptInterface
類中的公共方法中。
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();
}
}
}
對不起,但仍然無法與我:( – 2013-10-29 10:04:57