title: Displaying Cropped Image On ImageView
body: <p><strong>Overview</strong>: I have a simple layout with a button which when pressed.. opens up gallery telling me to choose a picture to crop, once picture is chosen, it goes into a cropping image screen. Once cropping of the image finishes and I hit "save/done" it should display the new cropped image onto my ImageView.</p>
  
  <p><strong>Issue</strong>: I can't seem to display the new cropped image onto the ImageView; when I hit "save" after I finish cropping the image, it goes back to my main layout indicating that it was saved but it never displays the image onto the view.</p>
  
  <p>This activity starts when a button is pressed and goes into the second activity. Once the picture from the second activity is acquired, it crops the image and should display it on the ImageView.</p>
  
  <pre><code>package com.example.pau.crop_test;
  
  import android.app.Activity;
  import android.content.ActivityNotFoundException;
  import android.content.Intent;
  import android.graphics.Bitmap;
  import android.net.Uri;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.ImageButton;
  import android.widget.ImageView;
  import android.widget.Toast;
  
  import java.io.File;
  
  public class ImageSelecter extends Activity {
  
      private final int GALLERY_ACTIVITY_CODE = 200;
      private final int RESULT_CROP = 400;
      private ImageView imageView2;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_crop);
  
          ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);
          Button profile_button = (Button) findViewById(R.id.button);
          profile_button.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  //Start Activity To Select Image From Gallery   
                  Intent gallery_Intent = new Intent(getApplicationContext(), GalleryUtil.class);
                  startActivityForResult(gallery_Intent, GALLERY_ACTIVITY_CODE);
  
              }
          });
  
      }
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if (requestCode == GALLERY_ACTIVITY_CODE) {
              if (resultCode == Activity.RESULT_OK) {
                  String picturePath = data.getStringExtra("picturePath");
                  //perform Crop on the Image Selected from Gallery
                  performCrop(picturePath);
              }
          }
  
          if (requestCode == RESULT_CROP) {
              if (resultCode == Activity.RESULT_OK) {
                  if (data != null) {
                      Bundle extras = data.getExtras();
                      if (extras != null) {
                          Bitmap selectedBitmap = extras.getParcelable("data");
                          // Set The Bitmap Data To ImageView
  
                          ImageView image =(ImageView) findViewById(R.id.imageView2);
                          image.setImageBitmap(selectedBitmap);
                         // imageView2.setScaleType(ImageView.ScaleType.FIT_XY);
                          }
                      }
                  }
              }
          }
  
  
      private void performCrop(String picUri) {
          try {
              //Start Crop Activity
  
              Intent cropIntent = new Intent("com.android.camera.action.CROP");
              // indicate image type and Uri
              File f = new File(picUri);
              Uri contentUri = Uri.fromFile(f);
  
              cropIntent.setDataAndType(contentUri, "image/*");
              // set crop properties
              cropIntent.putExtra("crop", "true");
              // indicate aspect of desired crop
              cropIntent.putExtra("aspectX", 1);
              cropIntent.putExtra("aspectY", 1);
              // indicate output X and Y
              cropIntent.putExtra("outputX", 280);
              cropIntent.putExtra("outputY", 280);
  
              // retrieve data on return
              cropIntent.putExtra("return-data", true);
              // start the activity - we handle returning in onActivityResult
              startActivityForResult(cropIntent, RESULT_CROP);
          }
          // respond to users whose devices do not support the crop action
          catch (ActivityNotFoundException anfe) {
              // display an error message
              String errorMessage = "your device doesn't support the crop action!";
              Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
              toast.show();
          }
      }
  </code></pre>
  
  <p>Below is the second activity which allows us to go into our gallery and choose a picture, then returns it to the first activity for cropping:</p>
  
  <pre><code>package com.example.pau.crop_test;
  
  import java.io.File;
  import android.app.Activity;
  import android.content.Intent;
  import android.database.Cursor;
  import android.net.Uri;
  import android.os.Bundle;
  import android.provider.MediaStore;
  import android.util.Log;
  
  public class GalleryUtil extends Activity{
      private final static int RESULT_SELECT_IMAGE = 100;
      public static final int MEDIA_TYPE_IMAGE = 1;
      private static final String TAG = "GalleryUtil";
  
      String mCurrentPhotoPath;
      File photoFile = null;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
  
          try{
              //Pick Image From Gallery
              Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
              startActivityForResult(i, RESULT_SELECT_IMAGE);
          }catch(Exception e){
              e.printStackTrace();
          }
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
          switch(requestCode){
              case RESULT_SELECT_IMAGE:
  
                  if (resultCode == Activity.RESULT_OK &amp;&amp; data != null &amp;&amp; data.getData() != null) {
                      try{
                          Uri selectedImage = data.getData();
                          String[] filePathColumn = {MediaStore.Images.Media.DATA };
                          Cursor cursor = getContentResolver().query(selectedImage,
                                  filePathColumn, null, null, null);
                          cursor.moveToFirst();
                          int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                          String picturePath = cursor.getString(columnIndex);
                          cursor.close();
  
                          //return Image Path to the Main Activity
                          Intent returnFromGalleryIntent = new Intent();
                          returnFromGalleryIntent.putExtra("picturePath",picturePath);
                          setResult(RESULT_OK,returnFromGalleryIntent);
                          finish();
                      }catch(Exception e){
                          e.printStackTrace();
                          Intent returnFromGalleryIntent = new Intent();
                          setResult(RESULT_CANCELED, returnFromGalleryIntent);
                          finish();
                      }
                  }else{
                      Log.i(TAG,"RESULT_CANCELED");
                      Intent returnFromGalleryIntent = new Intent();
                      setResult(RESULT_CANCELED, returnFromGalleryIntent);
                      finish();
                  }
                  break;
          }
      }
  }
  </code></pre>
  
  <p>Below is my layout which displays button and image view:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#FFFFFF"
      &gt;
  
      &lt;ImageView
          android:layout_width="200dp"
          android:layout_height="fill_parent"
          android:id="@+id/imageView2"
  
          android:layout_marginTop="41dp"
          android:contentDescription="@string/abc_activity_chooser_view_see_all" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/crop"
          android:id="@+id/button"
          android:layout_centerVertical="true"
          android:layout_toRightOf="@+id/imageView2"
          android:layout_toEndOf="@+id/imageView2" /&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>