2013-04-29 15 views
1

我試圖生成一個簡單的PDF並將其傳遞給Google雲打印,但是當我嘗試將PDF打印到我的谷歌驅動器時沒有任何反應。 (登錄作品) 我怎麼會認證我的PDF是完整且有效的,對於初學者?我覺得我正在考慮iText的話。iText /雲打印不能在Android上工作

另外,我得到一些錯誤,告訴我,

public class PDFViewer extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    this.getWindow().setSoftInputMode 
        (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
    super.onCreate(savedInstanceState); 

    InputStream object = this.getResources().openRawResource(R.raw.itextkey); 
    LicenseKey.loadLicenseFile(object); 

    File root = Environment.getExternalStorageDirectory(); 

    File f = new File(android.os.Environment.getExternalStorageDirectory() 
       .getAbsolutePath() + java.io.File.separator + "HelloWorld.pdf"); 
    boolean externalStorageAvailable = false; 
    boolean externalStorageWriteable = false; 
    String state = Environment.getExternalStorageState(); 
    // if we can read and write to storage 
    if (Environment.MEDIA_MOUNTED.equals(state)) { 
     externalStorageAvailable = true; 
     externalStorageWriteable = true; 
    } 
    // else if we can read but cannot write 
    else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
     externalStorageAvailable = true; 
     externalStorageWriteable = false; 
     } 
    if (externalStorageWriteable) { 
     System.out.println("enough storage!"); 
     // creation of a document-object 
     Document document = new Document(); 
     try { 
      // we create a writer that listens to the document 
      // and directs a PDF-stream to a file 
     if (f.exists()) 
      f.delete(); 
     try { 
      f.createNewFile(); 
     } catch (IOException e) {System.out.println(e);} 
     PdfWriter.getInstance(document, new FileOutputStream 
      (android.os.Environment.getExternalStorageDirectory() 
      .getAbsolutePath() + java.io.File.separator + "HelloWorld.pdf")); 
    // open + format the document 
     document.open(); 
      document.add(new Paragraph("Hello World")); 
     } catch (DocumentException de) { 
      System.err.println(de.getMessage()); 
     } catch (IOException ioe) { 
      System.err.println(ioe.getMessage()); 
     } 
     document.close(); 
    } 
    Intent printIntent = new Intent(this, PrintDialogActivity.class); 
    Uri hello = Uri.fromFile(f); 
    printIntent.setDataAndType(hello, "application/pdf"); 
    printIntent.putExtra("title", "stuff"); 
    startActivity(printIntent); 

我得到這個錯誤,我有0想法做些什麼:

04-29 03:41:18.502: E/chromium(749): external/chromium/net/disk_cache 
        /backend_impl.cc:1107: [0429/034118:ERROR:backend_impl.cc(1107)] 
        Critical error found -8 

我想知道我能嘗試玩這個工作。順便說一下,PrintDialogActivity來自Google網站的庫存。我關閉了應用程序設置中的Exchange,我已經允許將外部存儲寫入清單中,並且互聯網也已啓用。

感謝

+0

檢查iText是否正常工作的最簡單方法是檢查外部存儲器上的PDF。如果它是腐敗的,讀者會告訴你。如果您在PDF中看不到「Hello World」,請提供例外。 – 2013-04-29 07:55:35

+0

我用DDMS來檢查它,沒有顯示在「SD卡」文件夾中。有任何想法嗎? – user1993551 2013-04-29 14:37:31

+0

你的logcat顯示任何錯誤,異常,消息,什麼? – 2013-04-29 14:52:25

回答

6

我有同樣的問題,直到我發現了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(); 
     } 
    } 
}