title: Android, Compressing an image
body: <p>I am sending an image over the network via wifi or the mobile network to be stored in a server and retrieved again. I've done that but due to the size of images taken by the camera it's making my app slow, just to point out I'm opening the gallery and taking the pictures from there and not taking the picture directly from the app. I have noticed that images from whatsapp that have been taken from the camera and gallery have been compressed to approx. 100kb.</p>
  
  <p>At the moment my code takes a file and converts it to bytes and then sends it. Here is the method for taking a file and converting it to bytes.</p>
  
  <pre><code>private void toBytes(String filePath){
      try{
          File file = new File(filePath);
          InputStream is = new BufferedInputStream(new FileInputStream(file));  
          ByteArrayOutputStream buffer = new ByteArrayOutputStream();
          bytes = new byte[(int) filePath.length()];
          int bytes_read;
          while((bytes_read = is.read(bytes, 0, bytes.length)) != -1){
              buffer.write(bytes, 0, bytes_read);
          }
          is.close();               
          bytes = buffer.toByteArray();
      }catch(Exception err){
          Toast.makeText(getApplicationContext(), err.toString(), Toast.LENGTH_SHORT).show();
      }
  }
  </code></pre>
  
  <p>So my question is how would I go about compressing my image before sending? Also I don't need the image to retain a high pixel count as when the app uses the image it will only take up half of the devices screen.</p>
  
  <p>Thank you for any help given.</p>