Reputation: 3
i want convert PDF file to byte array in (onActivityResult) I tried several different ways but it didn't work Please answer if anyone knows.
Update :
case 1212 :
if (resultCode == RESULT_OK){
Uri uri = data.getData();
File file = new File(uri.getPath());
int size = (int) file.length();
byte[] bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(),"FileNotFound",Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(getApplicationContext(),"IOException",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}else Toast.makeText(getApplicationContext(),"خطا!",Toast.LENGTH_LONG).show();
this code show FileNotFound Toast
Upvotes: 0
Views: 868
Reputation: 545
Use Package java.nio.file from java 7
Path pdfFilePath = Paths.get("/file/path/your_file.pdf"); //File path
byte[] pdfByteArray = Files.readAllBytes(pdfFilePath );
Upvotes: 1