title: Android how to resize image in byte array format
body: <p>I have a camera activity. My activity will take a picture and then I have an AsyncTask to save the pic to the device. Here is my code:</p>
  
  <pre><code>class SavePhotoTask extends AsyncTask&lt;byte[], String, String&gt; {
  
      private ProgressDialog dialog;
  
      // protected Context applicationContext;
  
      @Override
      public void onPreExecute() {
  
          dialog = new ProgressDialog(CameraPreviewActivity.this);
  
          dialog.setMessage("gravando...");
          dialog.show();
  
      }
  
      @Override
      protected String doInBackground(byte[]... jpeg) {
  
          try {
  
              int i =jpeg[0].length;
              System.out.println("byte array size"+i);
  
              String timeStamp = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
  
              Random generator2 = new Random(10000);
              int r=generator2.nextInt(99999);
              File cacheDir = getDir(DIRECTORIO, Context.MODE_PRIVATE);
              String filename = timeStamp+"-" +r+ ".jpg";
              File file = new File(cacheDir, filename);
  
              FileOutputStream stream = new FileOutputStream(file);
              //FileOutputStream fos = new FileOutputStream(photo.getPath());
              stream.write(jpeg[0]);
              stream.close();
  
              long t=file.length();
              System.out.println("file size"+t);
          } catch (java.io.IOException e) {
              Log.e("PictureDemo", "Exception in photoCallback", e);
          }
  
          return (null);
      }
  
      @Override
      protected void onPostExecute(String result) {
          this.dialog.cancel();
  
      }
  }
  </code></pre>
  
  <p>(This class is a modification from the one in CommonsWare book (Advanced android development)</p>
  
  <p>So the SavePhotoTask receives the image in byte array format (byte[]) and saves it in a file.
  What I would like to do is to resize it (under 150K) and then save it if it is possible. I know I could create a bitmap but is there a way to compress the byte array directly? Or anyother class I could use.</p>
  
  <p>This is the code for resizing a bitmap:</p>
  
  <pre><code>Bitmap thumbnail=BitmapFactory.decodeByteArray(jpeg[0], 0, jpeg[0].length) 
  thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, stream);
          stream.close();
          long t=file.length();
          if(t&gt;Constants.MAX_FILE_SIZE){
              stream = new FileOutputStream(file);
              thumbnail.compress(Bitmap.CompressFormat.JPEG, 75, stream);
              stream.close();
              t=file.length();
              int i=50;
              while(t&gt;Constants.MAX_FILE_SIZE &amp;&amp; i&gt;=0){
                  stream = new FileOutputStream(file);
                  thumbnail.compress(Bitmap.CompressFormat.JPEG, i, stream);
                  stream.close();
                  i=i-5;
                  t=file.length();
              }
  
          }
  </code></pre>
  
  <p>is there a way I can create something better and of course using the minimum memory resources. thanks a lot</p>