首页 > 解决方案 > 在android中将PDF文件转换为字节数组

问题描述

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

更新 :

  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

标签: javaandroidfilepdf

解决方案


使用 java 7 中的包 java.nio.file

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

推荐阅读