Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

android - Getting Image from drawable and adding to PDF using iText

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);

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Of course it'll not work at that way.

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;
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...