1

我正在尝试生成一个简单的 PDF 并将其传递给 Google Cloud Print,但是当我尝试将 PDF 打印到我的谷歌驱动器时没有任何反应。(登录有效)对于初学者,我什至如何验证我的 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 是来自 Google 网站的库存。我在应用程序设置中关闭了 Exchange,我已经允许在清单中写入外部存储,并且 Internet 也已启用。

谢谢

4

1 回答 1

6

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

您应用的targetSdkVersionAndroidManifest.xml 中的 可能设置为 17 或更高。在这种情况下,您需要对PrintDialogActivity从 Google Developer 网站获得的内容进行小幅更改。您需要将注释添加@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-07-01T18:11:11.040 回答