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

Android API PdfDocument未使用writeTo(outputStream)在文件中写入数据,android,api,pdf,documents,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(); //

用于创建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创建的文件始终为空**

我在这里搜索了很多答案,但没有一个有效


是否有我可以解决此问题的方法请提供帮助哇哦3天&找到答案

protected File doInBackground(ArrayList<File>... params) {
        File output = null;
        PdfDocument doc = new PdfDocument();
        ArrayList<Bitmap> images = new ArrayList<Bitmap>();
        PdfDocument.Page page = null;
        ArrayList<Bitmap> bitmaps = null;
        Canvas canvas = null;
        File outputDir = new File(getFilesDir()+"/Output/PDF");
        if(!outputDir.exists()){
            outputDir.mkdirs();
        }
        Random rand = new Random();
        output = new File(getFilesDir()+"/Output/PDF/data-"+rand.nextInt()+".pdf");
        for(File a: params[0]){
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(a.getPath(), options);
            int imageHeight = options.outHeight;
            int imageWidth = options.outWidth;
            String imageType = options.outMimeType;
            Bitmap b = decodeSampledBitmapFromResource(a, imageWidth/2, imageHeight/2);
            if(b!=null){
                images.add(b);
            }
        }
        int i = 0;
        for(Bitmap a: images){
            PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(a.getWidth(), a.getHeight(), i++).create();
            page = doc.startPage(pageInfo);
            page.getCanvas().drawBitmap(a, 0, 0, new Paint(Paint.ANTI_ALIAS_FLAG));
            doc.finishPage(page);
        }
        try{
            FileOutputStream out = new FileOutputStream(output);
            doc.writeTo(out);
            out.flush();
            out.close();
        }catch(FileNotFoundException e){}
        catch(IOException e){}
        doc.close();
        return output;
    }
受保护文件doInBackground(ArrayList…params){
文件输出=null;
PdfDocument doc=新PdfDocument();
ArrayList images=新的ArrayList();
PdfDocument.Page=null;
ArrayList位图=空;
Canvas=null;
File outputDir=新文件(getFilesDir()+“/Output/PDF”);
如果(!outputDir.exists()){
outputDir.mkdirs();
}
Random rand=新的Random();
输出=新文件(getFilesDir()+”/output/PDF/data-“+rand.nextInt()+”.PDF”);
对于(文件a:params[0]){
BitmapFactory.Options=new-BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeFile(a.getPath(),选项);
int imageHeight=options.outHeight;
int imageWidth=options.outWidth;
字符串imageType=options.outMimeType;
位图b=decodeSampledBitmapFromResource(a,imageWidth/2,imageHeight/2);
如果(b!=null){
增加(b);
}
}
int i=0;
用于(位图a:图像){
PdfDocument.PageInfo PageInfo=新建PdfDocument.PageInfo.Builder(a.getWidth()、a.getHeight()、i++).create();
页面=文件起始页面(页面信息);
page.getCanvas().drawBitmap(一个,0,0,新的绘画(Paint.ANTI_别名_标志));
文件完成页(第页);
}
试一试{
FileOutputStream out=新的FileOutputStream(输出);
书面文件(发出);
out.flush();
out.close();
}catch(filenotfound异常){}
捕获(IOE){}
doc.close();
返回输出;
}