title: Can't set image to ImageView returned by camera
body: <p>I want to set the image returned from camera to the CropImageView.
  The library used is <a href="https://github.com/ArthurHub/Android-Image-Cropper" rel="nofollow">this</a>.</p>
  
  <p>The image returned by gallery is properly being set.</p>
  
  <p>But with the image returned by camera, when set with <code>setImageUri()</code>, the ImageView is empty although it shows the image's <code>Uri</code> in the Log.</p>
  
  <p>This is shown by Log in <code>onActivityResult</code></p>
  
  <p>file:///storage/emulated/0/Android/data/com.android.example/cache/pickImageResult.jpeg</p>
  
  <p>When trying to set with <code>setImageBitmap()</code>, <code>Bitmap photo = (Bitmap) data.getExtras().get("data")</code> results in <code>NullPointerException</code>.</p>
  
  <p>The code is</p>
  
  <pre><code>public class ImagePickerActivity extends AppCompatActivity {
  
  private CropImageView mCropImageView;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_image_picker);
      Log.d("image picker", "activity created");
      mCropImageView = (CropImageView) findViewById(R.id.cropImageView);
  
  }
  
  /**
   * On load image button click, start pick  image chooser activity.
   */
  public void onLoadImageClick(View view) {
      startActivityForResult(getPickImageChooserIntent(), 200);
  }
  
  /**
   * Crop the image and set it back to the  cropping view.
   */
  public void onCropImageClick(View view) {
      Bitmap cropped = mCropImageView.getCroppedImage(500, 500);
      if (cropped != null)
          mCropImageView.setImageBitmap(cropped);
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (resultCode == Activity.RESULT_OK) {
          Uri imageUri = getPickImageResultUri(data);
          Log.d("image picker", imageUri.toString());
  
          /*Bitmap photo = (Bitmap) data.getExtras().get("data");
          mCropImageView.setImageBitmap(photo );*/
  
          mCropImageView.setImageUri(imageUri);
      }
  }
  
  /**
   * Create a chooser intent to select the  source to get image from.&lt;br/&gt;
   * The source can be camera's  (ACTION_IMAGE_CAPTURE) or gallery's (ACTION_GET_CONTENT).&lt;br/&gt;
   * All possible sources are added to the  intent chooser.
   */
  public Intent getPickImageChooserIntent() {
  
      // Determine Uri of camera image to  save.
      Uri outputFileUri = getCaptureImageOutputUri();
  
      List&lt;Intent&gt; allIntents = new ArrayList&lt;&gt;();
      PackageManager packageManager = getPackageManager();
  
      // collect all camera intents
      Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
      List&lt;ResolveInfo&gt; listCam = packageManager.queryIntentActivities(captureIntent, 0);
      for (ResolveInfo res : listCam) {
          Intent intent = new Intent(captureIntent);
          intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
          intent.setPackage(res.activityInfo.packageName);
          if (outputFileUri != null) {
              intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
          }
          allIntents.add(intent);
      }
  
      // collect all gallery intents
      Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
      galleryIntent.setType("image/*");
      List&lt;ResolveInfo&gt; listGallery = packageManager.queryIntentActivities(galleryIntent, 0);
      for (ResolveInfo res : listGallery) {
          Intent intent = new Intent(galleryIntent);
          intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
          intent.setPackage(res.activityInfo.packageName);
          allIntents.add(intent);
      }
  
      // the main intent is the last in the  list so pickup the useless one
      Intent mainIntent = allIntents.get(allIntents.size() - 1);
      for (Intent intent : allIntents) {
          if (intent.getComponent().getClassName().equals("com.android.documentsui.DocumentsActivity")) {
              mainIntent = intent;
              break;
          }
      }
      allIntents.remove(mainIntent);
  
      // Create a chooser from the main  intent
      Intent chooserIntent = Intent.createChooser(mainIntent, "Select source");
  
      // Add all other intents
      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new Parcelable[allIntents.size()]));
  
      return chooserIntent;
  }
  
  /**
   * Get URI to image received from capture  by camera.
   */
  private Uri getCaptureImageOutputUri() {
      Uri outputFileUri = null;
      File getImage = getExternalCacheDir();
      if (getImage != null) {
  
          outputFileUri = Uri.fromFile(new File(getImage.getPath(), "pickImageResult.jpeg"));
      }
      return outputFileUri;
  }
  
  /**
   * Get the URI of the selected image from  {@link #getPickImageChooserIntent()}.&lt;br/&gt;
   * Will return the correct URI for camera  and gallery image.
   *
   * @param data the returned data of the  activity result
   */
  public Uri getPickImageResultUri(Intent data) {
      boolean isCamera = true;
      if (data != null) {
          String action = data.getAction();
          isCamera = action != null &amp;&amp; action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
      }
      return isCamera ? getCaptureImageOutputUri() : data.getData();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Please help to fix this issue. Thanks in advance.
  The code is taken from <a href="http://theartofdev.com/2015/02/15/android-cropping-image-from-camera-or-gallery/" rel="nofollow">here</a>.</p>