【问题标题】:Reading Pdf from URL and Parsing with Android PDF Viewer从 URL 读取 PDF 并使用 Android PDF Viewer 进行解析
【发布时间】:2014-04-18 22:11:03
【问题描述】:

我已经使用Android Pdf Viewerhttps://github.com/jblough/Android-Pdf-Viewer-Library 成功显示了来自Assets 文件夹的Pdf。我现在正在尝试解析和显示在线 pdf "http://www.gnostice.com/downloads/Gnostice_PathQuest.pdf" 但它给出了以下错误:

<code>
04-19 03:17:04.995: W/System.err(27806): java.io.IOException: This may not be a PDF File
04-19 03:17:04.995: W/System.err(27806):    at com.sun.pdfview.PDFFile.parseFile(PDFFile.java:1395)
04-19 03:17:04.995: W/System.err(27806):    at com.sun.pdfview.PDFFile.<init>(PDFFile.java:140)
04-19 03:17:04.995: W/System.err(27806):    at com.sun.pdfview.PDFFile.<init>(PDFFile.java:116)
04-19 03:17:04.995: W/System.err(27806):    at net.sf.andpdf.pdfviewer.PdfViewerActivity.openFile(PdfViewerActivity.java:909)
04-19 03:17:04.995: W/System.err(27806):    at net.sf.andpdf.pdfviewer.PdfViewerActivity$8.run(PdfViewerActivity.java:863)
04-19 03:17:04.995: W/System.err(27806):    at java.lang.Thread.run(Thread.java:1027)

</code>

我打开URL Connection 连接为:

<code>
fileUrl = new URL(filename);
                    HttpURLConnection connection = (HttpURLConnection)fileUrl.openConnection();
                    connection.connect(); 
                    InputStream is = connection.getInputStream();                   
                    byte[] bytes = new byte[is.available()];
                    is.read(bytes);
                    System.out.println("Byte Lenght: " + bytes.length);
                    ByteBuffer bb = ByteBuffer.NEW(bytes);
                    is.close();
                    openFile(bb, password);
</code>

请帮忙看看可能是什么问题?

谢谢

【问题讨论】:

    标签: httpurlconnection urlconnection pdfviewer


    【解决方案1】:

    您读取流的方式不正确。您可以使用这些实用程序类(取自DavidWebb):

    /**
     * Read an <code>InputStream</code> into <code>byte[]</code> until EOF.
     * <br/>
     * Does not close the InputStream!
     *
     * @param is the stream to read the bytes from
     * @return all read bytes as an array
     * @throws IOException
     */
    public static byte[] readBytes(InputStream is) throws IOException {
        if (is == null) {
            return null;
        }
        byte[] responseBody;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        copyStream(is, baos);
        baos.close();
        responseBody = baos.toByteArray();
        return responseBody;
    }
    
    /**
     * Copy complete content of <code>InputStream</code> to <code>OutputStream</code> until EOF.
     * <br/>
     * Does not close the InputStream nor OutputStream!
     *
     * @param input the stream to read the bytes from
     * @param output the stream to write the bytes to
     * @throws IOException
     */
    public static void copyStream(InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[1024];
        int count;
        while ((count = input.read(buffer)) != -1) {
            output.write(buffer, 0, count);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-10
      • 2020-12-09
      • 1970-01-01
      • 2018-10-08
      • 2021-12-27
      • 2014-09-04
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多