Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中将PDF文件转换为字节数组_Java_Android_File_Pdf - Fatal编程技术网

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

Java 在android中将PDF文件转换为字节数组,java,android,file,pdf,Java,Android,File,Pdf,我想在onActivityResult中将PDF文件转换为字节数组 我尝试了几种不同的方法,但都不起作用 如果有人知道,请回答 更新: case 1212 : if (resultCode == RESULT_OK){ Uri uri = data.getData(); File file = new File(uri.getPath());

我想在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();

此代码显示FileNotFoundToast

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

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

你为什么要这么做?如果pdf的长度为10或100 MB,该怎么办?uri.getPath通常不会返回文件路径。用于从Uri打开流,然后将其读入字节数组。但它无法正常工作。具体如何?