title: Android - Cropping after Camera and Gallery Intents Gives Strange Results
body: <p>I'm trying to launch an Android camera intent and a choose-photo intent (two buttons, one for take photo, one for choose from gallery) and both need a crop intent launched after them then have the cropped photo returned to my app's activity. I've gone through a bunch of the examples posted elsewhere, but I'm getting strange reults with my implementation.</p>
  
  <p>For the take photo event, it appears to work fine, except after taking a photo and going into crop mode, the wrong photo pops up. Instead of cropping the photo you just took it crops an older photo and I can't figure out where it's coming from. Also, sometimes after finishing the crop intent, it crashes with a nullpointerexception after Parcel.readException (can't always reproduce, but I think it haappens more if you take a picture and crop as quickly as possible).</p>
  
  <p>For the choose photo intent, your gallery pops up as expected but upon choosing a photo all that happens is the message "Saved" is toasted instead of returning to my app's activity with the image. I believe I have a misunderstanding of how the choose photo intent works (I pretty much reused the code for the take photo intent).</p>
  
  <p>In both cases, in crop mode you are still allowed to resize the cropped area despite having specified "scale" = false.</p>
  
  <p>My code is as follows:</p>
  
  <pre><code>public class TestPhotoActivity extends Activity {
  
  private ImageView imageView;
  private Uri imageUri;
  
  private int int_Height_crop = 600;
  private int int_Width_crop = 600;
  
  public final static int TAKE_PICTURE = 0;
  public final static int CHOOSE_PICTURE = 1;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.choose_photo); 
  
      imageView = (ImageView) findViewById(R.id.photo);
  
      Button take_photo = (Button) findViewById(R.id.take_photo);
      take_photo.setOnClickListener(new View.OnClickListener() {
          public void onClick(final View view) {  
              takePhoto(view);
          }
      });
  
      Button choose_photo = (Button) findViewById(R.id.choose_photo);
      choose_photo.setOnClickListener(new View.OnClickListener() {
          public void onClick(final View view) {  
              choosePhoto(view);
          }
      });     
  
  }
  
  public void takePhoto(View view) {      
      Intent intent = new Intent("android.media.action.IMAGE_CAPTURE", null);  
      intent.putExtra("crop", "true");
      intent.putExtra("aspectX", 1);
      intent.putExtra("aspectY", 1);
      intent.putExtra("outputX", int_Width_crop);
      intent.putExtra("outputY", int_Height_crop);
      intent.putExtra("scale", false);
      intent.putExtra("return-data", true);
      File photo = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");      
      intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
      imageUri = Uri.fromFile(photo);
      intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
      intent.putExtra("noFaceDetection", true);
      startActivityForResult(intent, TAKE_PICTURE);
  }
  
  public void choosePhoto(View view) {        
      Intent intent = new Intent(Intent.ACTION_PICK, null); 
      intent.setType("image/*");
      intent.putExtra("crop", "true");
      intent.putExtra("aspectX", 1);
      intent.putExtra("aspectY", 1);
      intent.putExtra("outputX", int_Width_crop);
      intent.putExtra("outputY", int_Height_crop);
      intent.putExtra("scale", false);
      intent.putExtra("return-data", true);
      File photo = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + "Pic.jpg");        
      intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
      imageUri = Uri.fromFile(photo);
      intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
      startActivityForResult(intent, CHOOSE_PICTURE);         
  }
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      switch (requestCode) {          
          case TAKE_PICTURE:              
              Log.d("photo", "requestCode: " + requestCode + "resultCode: " + resultCode + "wanted result: " + Activity.RESULT_OK);               
              if (resultCode == Activity.RESULT_OK) {
  
                  if (data == null) {
                      Log.w("photo", "Null data, but RESULT_OK, from image picker!");
                      Toast t = Toast.makeText(this, "No photo picked.", Toast.LENGTH_SHORT);
                      t.show();
                      return;
                  }
  
                  final Bundle extras = data.getExtras();
  
                  if (extras != null) {   
                      Log.d("photo", "extras is not null");
                      Uri selectedImage = imageUri;
                      getContentResolver().notifyChange(selectedImage, null);                
                      ContentResolver cr = getContentResolver();
                      Bitmap bitmap;
                      try {
                          bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);     
                          Log.d("photo", "data.getAction() is not null. setting image.");
                          imageView.setImageBitmap(bitmap);                           
                      } catch (Exception e) {
                          Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
                          Log.e("photo", e.toString());
                      }
                  }
              }
          case CHOOSE_PICTURE:
              Log.d("photo", "requestCode: " + requestCode + "resultCode: " + resultCode + "wanted result: " + Activity.RESULT_OK);
              if(resultCode == RESULT_OK){
                  Log.d("photo", "resultCode is ok");
  
                  if (data == null) {
                      Log.w("photo", "Null data, but RESULT_OK, from image picker!");
                      Toast t = Toast.makeText(this, "No photo picked.", Toast.LENGTH_SHORT);
                      t.show();
                      return;
                  }
  
                  final Bundle extras = data.getExtras();
  
                  if (extras != null) {   
                      Log.d("photo", "extras is not null");
                      Uri selectedImage = imageUri;
                      getContentResolver().notifyChange(selectedImage, null);                
                      ContentResolver cr = getContentResolver();
                      Bitmap bitmap;
                      try {
                          bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);  
                          Log.d("photo", "data.getAction() is not null. setting image.");
                          imageView.setImageBitmap(bitmap);
                      } catch (Exception e) {
                          Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
                          Log.e("photo", e.toString());
                      }
                  }                   
  
              }
      }
  }
  
  }
  </code></pre>
  
  <p>Any help is greatly appreciated!</p>
  
  <p>EDIT: I should also note that I'm testing on an LG Optimus LTE, Android 2.3</p>