在Android中将PDF文件转换为字节数组

时间:2019-09-03 06:17:42

标签: java android file pdf

我想将PDF文件转换为(onActivityResult)中的字节数组 我尝试了几种不同的方法,但是没有用 如果有人知道,请回答。

更新:

  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();

此代码显示FileNotFound Toast

1 个答案:

答案 0 :(得分:1)

使用来自Java 7的程序包java.nio.file

Path pdfFilePath = Paths.get("/file/path/your_file.pdf"); //File path
byte[] pdfByteArray = Files.readAllBytes(pdfFilePath );