Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.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
在Android中的PDF文档中添加位图图像_Pdf - Fatal编程技术网

在Android中的PDF文档中添加位图图像

在Android中的PDF文档中添加位图图像,pdf,Pdf,请问,如何将位图图像直接传递到pdf文件。我用GraphView制作了一个图形,最后在OnClickListener中将其转换为位图: write.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Bitmap bitmap; graph.setDrawingCacheEnabled(

请问,如何将位图图像直接传递到pdf文件。我用GraphView制作了一个图形,最后在OnClickListener中将其转换为位图:

write.setOnClickListener(new View.OnClickListener() 
{
        @Override
        public void onClick(View arg0) {
            Bitmap bitmap;
            graph.setDrawingCacheEnabled(true);
            bitmap = Bitmap.createBitmap(graph.getDrawingCache());
            graph.setDrawingCacheEnabled(false);
            String filename = "imagen";
            FileOperations fop = new FileOperations();
            fop.write(filename, bitmap);
            if (fop.write(filename,bitmap)) {
                Toast.makeText(getApplicationContext(),
                        filename + ".pdf created", Toast.LENGTH_SHORT)
                        .show();
            } else {
                Toast.makeText(getApplicationContext(), "I/O error",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });
问题是在FileOperations类中:

public FileOperations() {
}

public Boolean write(String fname, Bitmap bm) {
    try {
        String fpath = "/sdcard/" + fname + ".pdf";
        File file = new File(fpath);
        if (!file.exists()) {
            file.createNewFile();
        }
        Document document = new Document();
        PdfWriter.getInstance(document,
                new FileOutputStream(file.getAbsoluteFile()));
        document.open();
        String filename = bm.toString();
        com.itextpdf.text.Image image  =com.itextpdf.text.Image.getInstance(filename);
        document.add(image);
        document.add(new Paragraph("Hello World2!"));
        // step 5
        document.close();

        Log.d("Suceess", "Sucess");
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }
}
}
我想知道如何传递位图图像以将其添加到pdf文档中,我这样做了,但我认为只有当我给它一个路径时,这才有效

字符串文件名=bm.toString(); com.itextpdf.text.Image Image=com.itextpdf.text.Image.getInstance(文件名)

我只是解决了这个问题:

        document.open();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
        Image myImg = Image.getInstance(stream.toByteArray());
        myImg.setAlignment(Image.MIDDLE);
        document.add(myImg);

在FileOperations类中,位图bm=BitmapFactory.decodeResource(getResources(),R.mipmap.img_logo);