Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store base64 encoded string as file

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

like image 611
AhmedW Avatar asked Jun 07 '11 16:06

AhmedW


People also ask

How do I save a Base64 file?

How to convert Base64 to file. Paste your string in the “Base64” field. Press the “Decode Base64 to File” button. Click on the filename link to download the file.

How can I save a Base64 encoded image to disk?

Now in order to save the Base64 encoded string as Image File, the Base64 encoded string is converted back to Byte Array using the Convert. FromBase64String function. Finally the Byte Array is saved to Folder on Disk as File using WriteAllBytes method of the File class.


1 Answers

Perhaps you can flush the FileOutputStream before you close it.

os.write(pdfAsBytes);
os.flush();
os.close();
like image 161
Vito Limandibhrata Avatar answered Oct 07 '22 01:10

Vito Limandibhrata