Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert PDF to byte array and viceversa android

I'm trying to create an Android app able to upload and download PDF files to a server, I've already included in my code a method able to download a PDF from an URL such as "http://www.aaaa.com/myfile.pdf" and display it by using intent. But I want to know if there is a way to encode a Pdf into a byte array and viceversa decode a byte array to PDF file (I'm using mongoDB) Thanks to everyone :)

like image 330
AleConti99 Avatar asked Sep 10 '26 03:09

AleConti99


1 Answers

import java.nio.file.*;

Path pdfFilePath = Paths.get("/file/path/your_file.pdf");

// Read file to byte array
byte[] pdfByteArray = Files.readAllBytes(pdfFilePath );

// Write byte array to file 
Files.write(pdfFilePath , pdfByteArray);
like image 145
Kailash Avatar answered Sep 11 '26 17:09

Kailash