0

I already create table and some extra information as a PDF. But I couldn't import image to my PDF whatever I did. I have my_thumb.png file in assets folder.

in my MainActivity class

try { inputStream = MainActivity.this.getAssets().open("my_thumb.png"); Bitmap bmp = BitmapFactory.decodeStream(inputStream); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); model.setThumbStream(stream); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

in my CreatePDF class

Image thumb = Image.getInstance(Model.getThumbStream().toByteArray()); companyLogo.setAbsolutePosition(document.leftMargin(),121); companyLogo.scalePercent(25); document.add(thumb); 

and the problem is

bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 

this line returns with null.

What is the point that I missing?

Thanks in advance.

1 Answer 1

2

You first get the png to drawable

Drawable d = Drawable.createFromStream(getAssets().open("my_thumb.png"), null); 

then convert drawable to bitmap and get the byte array :)

Bitmap bitmap = ((BitmapDrawable)d).getBitmap(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] bitmapdata = stream.toByteArray(); 
Sign up to request clarification or add additional context in comments.

2 Comments

still it returns null.
I think your image not png,is it true?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.