0

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

3
  • 1
    why would you want to do that? what if your pdf is 10 or 100 MB long? Commented Sep 3, 2019 at 6:25
  • uri.getPath() won't return you the file path usually. Use developer.android.com/reference/android/content/… to open stream from Uri, and then read it into byte array. Commented Sep 3, 2019 at 6:30
  • but it didn't work how exactly? Commented Sep 3, 2019 at 6:50

1 Answer 1

1

Use Package java.nio.file from java 7

Path pdfFilePath = Paths.get("/file/path/your_file.pdf"); //File path byte[] pdfByteArray = Files.readAllBytes(pdfFilePath ); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.