从绘制图像获取和利用iText加入PDF [英] Getting Image from drawable and adding to PDF using iText

查看:156
本文介绍了从绘制图像获取和利用iText加入PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要添加图像利用iText的Andr​​oid PDF。我想要实现这个而不先保存图像的SD卡。我把我的形象进入RES /可绘制文件夹,但证明图片的路径不工作,它抛出FileNotFound异常。我的路径是这样的:

I want to add image to android PDF using iText. I want to achieve this without saving image to SDCard first. I put my image into res/drawable folder but proving the image path doesn’t work and it throws FileNotFound Exception. My path is like this:

String path = "res/drawable/myImage.png"
Image image = Image.getInstance(path);
document.add(image);

现在请建议我一个解决方案,我怎么会加重的getInstance(...)方法正确的文件路径。谢谢

Now please suggest me a solution how I will add correct file path to getInstance(…) method. Thanks

推荐答案

当然,它会不这样工作。

Of course it'll not work at that way.

将您的图像资源文件夹getassets访问它()方法

move your image to assets folder to access it with getassets() method

// load image
    try {
            // get input stream
           InputStream ims = getAssets().open("myImage.png");
           Bitmap bmp = BitmapFactory.decodeStream(ims);
           ByteArrayOutputStream stream = new ByteArrayOutputStream();
           bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
           Image image = Image.getInstance(stream.toByteArray());
           document.add(image);
        }
   catch(IOException ex)
        {
            return;
        }

这篇关于从绘制图像获取和利用iText加入PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆