2014-04-18 4 views
0

Android Pdf Viewer 라이브러리 https://github.com/jblough/Android-Pdf-Viewer-Library을 사용하여 Assets 폴더에서 Pdf를 성공적으로 표시했습니다. 지금은 "http://www.gnostice.com/downloads/Gnostice_PathQuest.pdf"구문 분석하고 온라인 PDF를 표시하려고하지만, 다음과 같은 오류주고있다 :URL에서 PDF 읽기 및 Android로 구문 분석 PDF 뷰어

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

이 문제가 될 수있는 것을 도와주세요 : 나뿐만 URL Connection 연결을 여는하고

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

를?

감사합니다.

답변

0

스트림을 읽는 방법이 올바르지 않습니다. 다음 유틸리티 클래스를 사용할 수 있습니다 (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); 
    } 
}