1

I am trying to save an input stream from a HTTPSUrlConnection to a PDF file stored on the device. It looks like it goes smoothly. However, when I loads the document in PDFView, I receive the following error: PDF is corrupted.

private void saveAsPDF(DataInputStream inputStream, final String filename)
{
    try {

        FileOutputStream fileOutputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
        fileOutputStream.write(IOUtils.toByteArray(inputStream));
        fileOutputStream.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

And here is how I load the pdf document in PDFView:

private void loadPDFData()
{
    File file = new File(princhDocument.getPath());

    pdfView.fromFile(file).onLoad(new OnLoadCompleteListener() {
        @Override
        public void loadComplete(int i) {
            princhDocument.setNumberOfPages(pdfView.getPageCount());

            pdfView.recycle();
        }
    }).load();
}
7heViking
  • 7,137
  • 11
  • 50
  • 94
  • can you post `IOUtils.toByteArray` ? What is `princhDocument.getPath()` ? – Blackbelt Jun 11 '15 at 13:33
  • @Blackbelt Yes. IOUtils.toByteArray is comming from apaches commons-io library which you can see here: https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/IOUtils.html#toByteArray(java.io.InputStream) and the path is a string of the path to the file. – 7heViking Jun 11 '15 at 13:41
  • @Blackbelt The path is received like this: context.getFilesDir().getPath() – 7heViking Jun 11 '15 at 13:42
  • 1
    you probably need the path to the file you saved. Try with `context. getFileStreamPath(filename.pdf).getPath()` – Blackbelt Jun 11 '15 at 13:56

0 Answers0