title: Add an Image in an iText PDF document in Android Studio
body: <p>I am trying to add an image in my iText PDF document in Android Studio, with Java, but it always shows the error <em>NullPointerException</em>.</p>
  
  <p>The codes i've trying are:</p>
  
  <p>1.</p>
  
  <pre><code>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();
      }
  </code></pre>
  
  <p>2.</p>
  
  <pre><code>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();
      }
  </code></pre>
  
  <p>3.</p>
  
  <pre><code>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();
      }
  </code></pre>
  
  <p>4.</p>
  
  <pre><code>try {
          Image image = Image.getInstance("res/drawable/logo.png");
          return image;
      } catch (BadElementException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>..and no one of those codes are working. Always the same error, not founding the resource.</p>
  
  <p>My question is, can I add an image to an iText doc? How can I do this?</p>
  
  <p>Ps. I'm using iText5 (<em>implementation 'com.itextpdf:itextg:5.5.10'</em>).</p>