Android API PdfDocument不使用writeTo(outputStream)在文件中写入数据

时间:2017-03-18 04:52:36

标签: android api pdf documents

创建pdf文件的按钮

//button listener for creating pdf file

convert2PDF.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
//pdf document created

            PdfDocument document = new PdfDocument();
//pageinfo created 

            PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(800, 800, 1).create();
//page created based on pageinfo

            PdfDocument.Page page = document.startPage(pageInfo);
//ImageView used to draw bitmap on page's canvas

            ImageView imageView = new ImageView(context);
//setting imageView height weight equal to page height weight

            imageView.setMaxHeight(800);
            imageView.setMaxWidth(800);
//setting the imageView to bitmap selected from list
     imageView.setImageBitmap(BitmapFactory.decodeFile(selectedFileList.get(0).getPath()));
//imageView drawn on canvas
            imageView.draw(page.getCanvas());
//page finished 
            document.finishPage(page);

            File result = null;
            try {

                result = File.createTempFile("afew", ".pdf", 
context.getCacheDir());
//writing the pdf doc to temp file
                document.writeTo(new BufferedOutputStream(new 
FileOutputStream(result)));
            } catch (FileNotFoundException e) {
                Toast.makeText(getApplicationContext(), e.getMessage(), 
Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(), 
e.getMessage(),Toast.LENGTH_SHORT).show();
            }
            document.close();
//main file 

            File a = new File("/storage/sdcard/b.pdf");
//funct transfer used for transferring bytes from one file to another
            transfer(result, a);
//set content of layout with imageView

            context.setContentView(imageView);
    }});

**我尝试在代码上运行,但在/storage/sdcard/b.pdf创建的文件总是为空**

我在这里搜索了许多答案,但没有一个工作

无论如何我可以解决这个问题PLZ帮助

1 个答案:

答案 0 :(得分:0)

哇3天&找到答案

const result = Object.keys(rooms).reduce((obj, roomKey) => {
  obj[roomKey] = rooms[roomKey].slice().sort((a, b) => a.RoomName > b.RoomName);
  return obj;
}, {});