2013-10-29 60 views
0

我有一個簡單的應用程序在Android中。其中有兩個activity.One是Document.Activity與佈局xml是butt.xml。另一個是printDialogActivity和print_dialog.xml。我共享我的打印機,一切正常。但是,當我選擇我的共享打印機並單擊谷歌雲打印中的打印按鈕時,會出現一個帶有「缺少文檔」的黑色標題欄。現在我能做什麼?請幫我..文檔丟失的Android谷歌雲打印

這裏是我的文檔活動:

public class Document extends Activity{ 

Button btnprt; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.butt); 

    btnprt = (Button) findViewById(R.id.button1); 
    btnprt.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
       Intent printIntent = new Intent(Document.this, PrintDialogActivity.class); 
       //File file = new File("/sdcard/StudentLatePass.txt"); 
       //printIntent.setDataAndType(Uri.fromFile(file), "text/*"); 
       Uri docUri = Uri.parse("http://www.google.com"); 
       //String ur = docUri.toString(); 
       printIntent.setDataAndType(docUri, "application/pdf"); 
       printIntent.putExtra("title", "StudentLatePass"); 
       startActivity(printIntent); 
     } 
    }); 
} 

}

這裏是我的butt.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="114dp" 
    android:text="Print" /> 

    </RelativeLayout> 

這裏是我的PrintDialogActivity:

public class PrintDialogActivity extends Activity { 
    private static final String PRINT_DIALOG_URL = "https://www.google.com/cloudprint  /dialog.html"; 
    private static final String JS_INTERFACE = "AndroidPrintDialog"; 
    private static final String CONTENT_TRANSFER_ENCODING = "base64"; 

    private static final String ZXING_URL = "http://zxing.appspot.com"; 
    private static final int ZXING_SCAN_REQUEST = 65743; 

    /** 
    * Post message that is sent by Print Dialog web page when the printing dialog 
    * needs to be closed. 
    */ 
    private static final String CLOSE_POST_MESSAGE_NAME = "cp-dialog-on-close"; 

    /** 
    * Web view element to show the printing dialog in. 
    */ 
    private WebView dialogWebView; 

    /** 
    * Intent that started the action. 
    */ 
    Intent cloudPrintIntent; 

    @Override 
    public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 

    setContentView(R.layout.print_dialog); 
    dialogWebView = (WebView) findViewById(R.id.webview); 
    cloudPrintIntent = this.getIntent(); 

    WebSettings settings = dialogWebView.getSettings(); 
    settings.setJavaScriptEnabled(true); 

    dialogWebView.setWebViewClient(new PrintDialogWebClient()); 
    dialogWebView.addJavascriptInterface(
     new PrintDialogJavaScriptInterface(), JS_INTERFACE); 

    dialogWebView.loadUrl(PRINT_DIALOG_URL); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    if (requestCode == ZXING_SCAN_REQUEST && resultCode == RESULT_OK) { 
     dialogWebView.loadUrl(intent.getStringExtra("SCAN_RESULT")); 
    } 
    } 

    final class PrintDialogJavaScriptInterface { 
    public String getType() { 
     return cloudPrintIntent.getType(); 
    } 

    public String getTitle() { 
     return cloudPrintIntent.getExtras().getString("title"); 
    } 

    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 ""; 
    } 

    public String getEncoding() { 
     return CONTENT_TRANSFER_ENCODING; 
    } 

    public void onPostMessage(String message) { 
     if (message.startsWith(CLOSE_POST_MESSAGE_NAME)) { 
     finish(); 
     } 
    } 
    } 

    private final class PrintDialogWebClient extends WebViewClient { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     if (url.startsWith(ZXING_URL)) { 
     Intent intentScan = new Intent("com.google.zxing.client.android.SCAN"); 
     intentScan.putExtra("SCAN_MODE", "QR_CODE_MODE"); 
     try { 
      startActivityForResult(intentScan, ZXING_SCAN_REQUEST); 
     } catch (ActivityNotFoundException error) { 
      view.loadUrl(url); 
     } 
     } else { 
     view.loadUrl(url); 
     } 
     return false; 
    } 

    @Override 
    public void onPageFinished(WebView view, String url) { 
     if (PRINT_DIALOG_URL.equals(url)) { 
     // Submit print document. 
     view.loadUrl("javascript:printDialog.setPrintDocument(printDialog.createPrintDocument(" 
      + "window." + JS_INTERFACE + ".getType(),window." + JS_INTERFACE + ".getTitle()," 
      + "window." + JS_INTERFACE + ".getContent(),window." + JS_INTERFACE + ".getEncoding()))"); 

     // Add post messages listener. 
     view.loadUrl("javascript:window.addEventListener('message'," 
      + "function(evt){window." + JS_INTERFACE + ".onPostMessage(evt.data)}, false)"); 
     } 
    } 
    } 
} 

這是我的打印_dialog.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
<WebView android:id="@+id/webview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 

+0

有沒有人可以幫我???????????? –

+0

Plz plz幫我......... –

+0

我正在反對同一個問題。如果我弄清楚,我會讓你知道的。 –

回答

0

如果您想要打印的網站內容,在圖像形式複製的位圖流成PDF格式。您可以使用開源庫在android中創建pdf。有很多庫可用來創建pdf。現在,您可以打印此PDF -

烏里docUri = Uri.parse(yourPDFFilePath); //給出你的路徑pdf文件 printIntent.setDataAndType(docUri,「application/pdf」);

其良好的使用PDF格式打印文檔作爲谷歌雲打印服務提供商推薦使用它 - 「我們目前提供對PDF文檔的支持最好,所以我們鼓勵你比別人使用這種格式。」

我試過這樣&已成功打印的網頁內容。希望能幫助到你 !!!

+0

謝謝。我能夠解決這個問題。 –

+0

嗨, 你用同樣的方式或任何其他方式?我很好奇,因爲我遵循的方法使用addJavascriptInterface()&我正在尋找其他方法來避免使用此方法。 (其實,我想使原生的Android方法調用此打印功能,但避免使用addJavascriptInterface()的) –

+0

其實我第一次採取的WebView的截圖並將其保存到外部存儲,然後我把它加入到一個PDF文檔(itext庫),並最後用谷歌雲打印該文檔。但是我有一個問題,截圖不能給出完整的webview,所以我使用了webview.capturePicture()。你來自哪裏? –