iText / Cloud Print无法在Android上运行

时间:2013-04-29 03:55:18

标签: java android eclipse itext google-cloud-print

我正在尝试生成一个简单的PDF并将其传递给Google Cloud Print,但是当我尝试将PDF打印到我的Google驱动器时没有任何反应。 (登录工作) 对于初学者,我如何验证我的PDF是完整有效的?我觉得我正在接受iText的话。

另外,我收到一些错误告诉我

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);

我收到此错误,我知道该怎么做:

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是谷歌网站上的库存。我在应用程序设置中关闭了Exchange,我允许在清单中写入外部存储,并且还启用了互联网。

由于

1 个答案:

答案 0 :(得分:6)

我遇到了同样的问题,直到我在Android开发者网站上找到了解决方案:http://developer.android.com/guide/webapps/webview.html

您应用的AndroidManifest.xml中的targetSdkVersion可能设置为17或更高。在这种情况下,您需要对从Google Developer网站获得的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();
        }
    }
}