I am trying to add an image in my iText PDF document in Android Studio, with Java, but it always shows the error NullPointerException.
The codes i've trying are:
1.
try { InputStream inputStream = context.getAssets().open("res/drawable/logo.png"); Bitmap bitmapA = BitmapFactory.decodeStream(inputStream); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmapA.compress(Bitmap.CompressFormat.PNG, 100, stream); Image image = Image.getInstance(stream.toByteArray()); return image; }catch (Exception e){ e.printStackTrace(); } 2.
try { Drawable d = context.getResources().getDrawable(R.drawable.logo); 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()); return image; }catch (Exception e){ e.printStackTrace(); } 3.
try { Drawable d = context.getDrawable(R.drawable.logo); 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()); return image; }catch (Exception e){ e.printStackTrace(); } 4.
try { Image image = Image.getInstance("res/drawable/logo.png"); return image; } catch (BadElementException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } ..and no one of those codes are working. Always the same error, not founding the resource.
My question is, can I add an image to an iText doc? How can I do this?
Ps. I'm using iText5 (implementation 'com.itextpdf:itextg:5.5.10').