title: Android How to get image saved on sdcard, resize it and save it again?
body: <p>i have an issue, i'm using the following code to take a picture:</p>
  
  <pre><code>private void captureImage() {
              Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  
              fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
  
              intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
  
              // start the image capture Intent
              startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
          }
  </code></pre>
  
  <p>the function to create the fileUri is this:</p>
  
  <pre><code>public Uri getOutputMediaFileUri(int type) {
              return Uri.fromFile(getOutputMediaFile(type));
          }
  
          /*
           * returning image / video
           */
          private static File getOutputMediaFile(int type) {
  
              // External sdcard location
              File mediaStorageDir = new File(
                      Environment
                              .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                      IMAGE_DIRECTORY_NAME);
  
              // Create the storage directory if it does not exist
              if (!mediaStorageDir.exists()) {
                  if (!mediaStorageDir.mkdirs()) {
                      Log.d(IMAGE_DIRECTORY_NAME, "Creación fallida "
                              + IMAGE_DIRECTORY_NAME + " directory");
                      return null;
                  }
              }
  
              // Create a media file name
              String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                      Locale.getDefault()).format(new Date());
              File mediaFile;
              if (type == MEDIA_TYPE_IMAGE) {
                  mediaFile = new File(mediaStorageDir.getPath() + File.separator
                          + "IMGTRAF_" + timeStamp + ".jpg");
              } else if (type == MEDIA_TYPE_VIDEO) {
                  mediaFile = new File(mediaStorageDir.getPath() + File.separator
                          + "VIDTRAF_" + timeStamp + ".mp4");
              } else {
                  return null;
              }
  
              return mediaFile;
          }
  </code></pre>
  
  <p>and here i'm showing the image on a ImageView:</p>
  
  <pre><code>@Override
          protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              // if the result is capturing Image
              if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
                  if (resultCode == RESULT_OK) {
                      // successfully captured the image
                      // display it in image view
                      previewCapturedImage();
                      Toast.makeText(getApplicationContext(),
                              "Mostrando foto", Toast.LENGTH_SHORT)
                              .show();
                  } else if (resultCode == RESULT_CANCELED) {
                      // user cancelled Image capture
                      Toast.makeText(getApplicationContext(),
                              "Acción cancelada", Toast.LENGTH_SHORT)
                              .show();
                  } else {
                      // failed to capture image
                      Toast.makeText(getApplicationContext(),
                              "Error al capturar la imagen", Toast.LENGTH_SHORT)
                              .show();
                  }
              } else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
                  if (resultCode == RESULT_OK) {
                      // video successfully recorded
                      // preview the recorded video
                      previewVideo();
                      Toast.makeText(getApplicationContext(),
                              "Reproduciendo video", Toast.LENGTH_SHORT)
                              .show();
                  } else if (resultCode == RESULT_CANCELED) {
                      // user cancelled recording
                      Toast.makeText(getApplicationContext(),
                              "Acción cancelada", Toast.LENGTH_SHORT)
                              .show();
                  } else {
                      // failed to record video
                      Toast.makeText(getApplicationContext(),
                              "Error al capturar el video", Toast.LENGTH_SHORT)
                              .show();
                  }
              }
          }
  
          /*
           * Display image from a path to ImageView
           */
          private void previewCapturedImage() {
              try {
                  // hide video preview
                  videoPreview.setVisibility(View.GONE);
  
                  imgPreview.setVisibility(View.VISIBLE);
  
                  // bimatp factory
                  BitmapFactory.Options options = new BitmapFactory.Options();
  
                  // downsizing image as it throws OutOfMemory Exception for larger
                  // images
                  options.inSampleSize = 8;
  
                  final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                          options);
  
                  imgPreview.setImageBitmap(bitmap);
              } catch (NullPointerException e) {
                  e.printStackTrace();
              }
          }
  </code></pre>
  
  <p>The problem is that when i capture the image it's being saved in it's original size (1954*2560) and when i upload it to the server takes a very long time. I've saw many questions and answers here but none worked for me. I want to take the photo, resize it to 800x600 and then save it again so i can upload it to the server.</p>
  
  <p>How can i achieve that? Please consider that i'm very new to Android programming.</p>
  
  <p>Thanks!</p>
  
  <p>Finally i've resolved the problem by doing:</p>
  
  <pre><code>private void previewCapturedImage() {
              try {
                  // hide video preview
                  videoPreview.setVisibility(View.GONE);
  
                  imgPreview.setVisibility(View.VISIBLE);
  
                  // bimatp factory
                  BitmapFactory.Options options = new BitmapFactory.Options();
  
                  // downsizing image as it throws OutOfMemory Exception for larger
                  // images
                  options.inSampleSize = 8;
  
                  final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                          options);
  
                  File mediaStorageDir = new File(
                          Environment
                                  .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                          IMAGE_DIRECTORY_NAME);
  
                  String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                          Locale.getDefault()).format(new Date());
                  File file;
                  file = new File(fileUri.getPath());
                  try {
                      FileOutputStream out = new FileOutputStream(file);
                      bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                      out.flush();
                      out.close();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
  
                  //fileUri = file;
  
                  imgPreview.setImageBitmap(bitmap);
              } catch (NullPointerException e) {
                  e.printStackTrace();
              }
          }
  </code></pre>
  
  <p>I hope it helps someone in the near future.</p>