title: Reading PNG file: BitmapFactory vs ImageIO
body: <p>I'm trying to read and modify a png image using ImageIO, and read it as a drawable resource in Android.</p>
  
  <p>Now the java code looks like this:</p>
  
  <pre><code>BufferedImage im = ImageIO.read(new File(inFilePath));
  WritableRaster raster = image.getRaster();
  DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();
  byte imBytes[] = buffer.getData();
  </code></pre>
  
  <p>This gives me a byte array with size about 140k, for the sample file.
  Next I try to read it with BitmapFactory:</p>
  
  <pre><code>Drawable drawable = context.getResources().getDrawable(getImageResourceIdWithKeys());
  Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  bitmap.compress(Bitmap.CompressFormat.PNG, 200, stream);
  byte[] imBytes = stream.toByteArray();
  </code></pre>
  
  <p>This gives me a byte array with size about 52k, next reading it as a raw resource gives about 24k which is the size on disk. Now I understand that the PNG file is compressed on the disk, but I can't understand why I get different byte arrays when getting the uncompressed version. Is there some way to read the image in the same way on Android and using standard Java API ?</p>