title: Android: Resizing Bitmaps without losing quality
body: <p>i've really searched through over the entire web before posting. My problem is that i cannot resize bitmap without losing the quality of the image (the quality is really bad and pixelated).</p>
  
  <p>I take the bitmap from camera and then i have to downscale it, so i can upload it to the server much faster.</p>
  
  <p>This is the function that does the sampling</p>
  
  <pre><code>public Bitmap resizeBitmap(Bitmap bitmap){
           Canvas canvas = new Canvas();
           Bitmap resizedBitmap = null;
           if (bitmap !=null) {
                  int h = bitmap.getHeight();
                  int w = bitmap.getWidth();
                  int newWidth=0;
                  int newHeight=0;
  
                  if(h&gt;w){
                      newWidth = 600;
                      newHeight = 800;
                  }
  
                  if(w&gt;h){
                      newWidth = 800;
                      newHeight = 600;
                      }
  
                  float scaleWidth = ((float) newWidth) / w;
                  float scaleHeight = ((float) newHeight) / h;
  
  
  
                  Matrix matrix = new Matrix();
                  // resize the bit map
                  matrix.preScale(scaleWidth, scaleHeight);
  
                  resizedBitmap  = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
  
  
  
                  Paint paint = new Paint();
                  paint.setAntiAlias(true);
                  paint.setFilterBitmap(true);
                  paint.setDither(true);
  
                  canvas.drawBitmap(resizedBitmap, matrix, paint);
  
  
  
              }
  
  
          return resizedBitmap;   
  </code></pre>
  
  <p>and this is how i get the image from activity result</p>
  
  <pre><code> protected void onActivityResult(int requestCode, int resultCode,
               Intent data) {
           if(resultCode != RESULT_CANCELED){
  
           if (requestCode == 0) {
               if (resultCode == RESULT_OK) {
  
  
                      getContentResolver().notifyChange(mImageUri, null);
                      ContentResolver cr = this.getContentResolver();
  
                      try
                      {
                          b = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);
                          Log.d("foto", Integer.toString(b.getWidth()));
                          Log.d("foto", Integer.toString(b.getHeight()));
                          addPhoto.setImageBitmap(b);
  
                      }
                      catch (Exception e)
                      {
                          Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
                          Log.d("TAG", "Failed to load", e);
                      }
  
  
  
               }
           }
  </code></pre>
  
  <p>I'm starting to think that the best way to get a small picture size is to set the camera resolution. Anyone else can help?</p>