5

I have a web service that returns a base64 encoded string of a PDF file.

I want to save this file to the SD Card. but when i try do this, adobe reader tells me that the file is corrupt. obviously i am not saving it properly.

byte[] pdfAsBytes = Base64.decode(resultsRequestSOAP.toString(), 0);

File filePath = new File(Environment.getExternalStorageDirectory()+"/braodcasts.pdf");
FileOutputStream os = new FileOutputStream(filePath, true);
os.write(pdfAsBytes);
os.close();

resultsRequestSOAP.toString() looks like this:

JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFuZyhlbi1JRSkgPj4NCmVuZG9iag0KMiAwIG9iag0KPDwvVHlwZS9QYWdlcy9Db3VudCAxL0tpZHNbIDMgMCBSXSA+Pg0KZW5kb2JqDQozIDAgb2JqDQo8PC9UeXBlL1BhZ2UvUGFyZW50IDIgMCBSL1Jlc291cmNlczw8L0ZvbnQ8PC9GMSA1IDAgUj4+L0V4dEdTdGF0ZTw8L0dTNyA3IDAgUi9HUzggOCAwIFI+Pi9YT2JqZWN0PDwvSW1hZ2U5IDkgMCBSPj4vUHJvY1NldFsvUERGL1RleHQvSW1hZ2VCL0ltYWdlQy9JbWFnZUldID4+L01lZGlhQm94WyAwIDAgNTk1LjMyIDQxOS41Ml0gL0NvbnRlbnRzIDQgMCBSL0dyb3VwPDwvVHlwZS9Hcm91cC9TL1RyYW5zcGFyZW5jeS9DUy9EZXZpY2VSR0I+Pi9UYWJzL1M+Pg0KZW5kb2JqDQo0IDAgb2JqDQo8PC9GaWx0ZXIvRmxhdGVEZWNvZGUvTGVuZ3RoIDE0ND4+DQpzdHJlYW0NCnicLY29CgIxEIT7hX2HKVVw3ewluUsr/qCdGLAQC5HzqlPU9wdjcKaaYZhvmZkWGwfnRD3ynclBix18l6T0TRJ1yGOZbY8thg+TYqip+6ct03mC6QV5z7Quhy+muYpWWWjhLEqKCClIYzBTCRHvnuk0w4PJuyi+Uk07MbRWsWV6+2F343XoE1ZPHJi+nDkiaw0KZW5kc3RyZWFtDQplbmRvYmoNCjUgMCBvYmoNCjw8L1R5cGUvRm9udC9TdWJ0eXBlL1RydWVUeXBlL05hbWUvRjEvQmFzZUZvbnQvQUJDREVFK0NhbGlicmkvRW5jb2RpbmcvV2luQW5zaUVuY29kaW5nL0ZvbnREZXNjcmlwdG9yIDYgMCBSL0ZpcnN0Q2hhciAzMi9MYXN0Q2hhc

Thats just a snippet!

Thanks

5
  • It looks like you're opening it for appending. Is there already a file with that name to which you're appending?
    – MRAB
    Jun 7, 2011 at 16:49
  • i'm not opening it for append, i'm overwriting the file each time
    – AhmedW
    Jun 7, 2011 at 16:56
  • Have a look at the PDF in a text editor. The first line should be something like "%PDF-1.4" and the last line should be "%%EOF".
    – MRAB
    Jun 7, 2011 at 17:03
  • yeah it is, i just wasn't flushing the file after writing. Thanks for your help :)
    – AhmedW
    Jun 7, 2011 at 17:16
  • I just noticed all pdf's start with JVBERi0xLj. And I was only using the first part to check if my pdf changed or not...
    – miva2
    Dec 29, 2015 at 10:14

1 Answer 1

12

Perhaps you can flush the FileOutputStream before you close it.

os.write(pdfAsBytes);
os.flush();
os.close();
3
  • 3
    That surprises me. I would've expected that close would automatically flush any unwritten bytes.
    – MRAB
    Jun 7, 2011 at 18:11
  • the flush() method in FileOutputStream does nothing, it is inherited from OutputStream. As the doc says Flushes this stream. Implementations of this method should ensure that any buffered data is written out. This implementation does nothing.
    – miva2
    Dec 29, 2015 at 14:44
  • I think it flushes out any remaining bytes that might be in the stream, which explains why it fixed my issue.
    – AhmedW
    Jan 13, 2016 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.