Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 452
naeemgik Avatar asked Apr 01 '13 10:04

naeemgik


People also ask

How do I save a drawable image?

Step 1: In this method first of all in your system find your required images and copy the image as we do normally. Step 2: Then open the Android Studio go to the app > res > drawable > right-click > Paste as shown in the below figure.


Video Answer


1 Answers

I found a solution for your issue. If you want to get image from your drawable folder and put it into a PDF file using iText use this code:

try {
    document.open();
    Drawable d = getResources().getDrawable(R.drawable.myImage);
    BitmapDrawable bitDw = ((BitmapDrawable) d);
    Bitmap bmp = bitDw.getBitmap();  
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    Image image = Image.getInstance(stream.toByteArray());
    document.add(image);    
    document.close();
} catch (Exception e) {
      e.printStackTrace();
}
like image 60
Tony86 Avatar answered Sep 28 '22 04:09

Tony86