0

I want to set the image returned from camera to the CropImageView. The library used is this.

The image returned by gallery is properly being set.

But with the image returned by camera, when set with setImageUri(), the ImageView is empty although it shows the image's Uri in the Log.

This is shown by Log in onActivityResult

file:///storage/emulated/0/Android/data/com.android.example/cache/pickImageResult.jpeg

When trying to set with setImageBitmap(), Bitmap photo = (Bitmap) data.getExtras().get("data") results in NullPointerException.

The code is

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.<br/>
 * The source can be camera's  (ACTION_IMAGE_CAPTURE) or gallery's (ACTION_GET_CONTENT).<br/>
 * All possible sources are added to the  intent chooser.
 */
public Intent getPickImageChooserIntent() {

    // Determine Uri of camera image to  save.
    Uri outputFileUri = getCaptureImageOutputUri();

    List<Intent> allIntents = new ArrayList<>();
    PackageManager packageManager = getPackageManager();

    // collect all camera intents
    Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    List<ResolveInfo> 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<ResolveInfo> 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()}.<br/>
 * 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 && action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
    }
    return isCamera ? getCaptureImageOutputUri() : data.getData();
}

}

Please help to fix this issue. Thanks in advance. The code is taken from here.

2 Answers2

0

For getting image from camera I am using below code, its working in lollipop also:

    private void captureFromCamera()
                Calendar calendar = Calendar.getInstance();
                String fileName = String.valueOf(calendar.getTimeInMillis());
                fileName += ".jpg";

                ContentValues values = new ContentValues();

                values.put(MediaStore.Images.Media.TITLE, fileName);

                values.put(MediaStore.Images.Media.DESCRIPTION,
                        "Image capture by camera");

                Uri imageUri = activity.getContentResolver().insert(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

                new DevicePreferences().addKey(activity, Constants.IMAGE_URI,
                        imageUri.toString());  // Storing in shared preference and in onActivityResult getting uri from shared preference

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

                intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

                startActivityForResult(intent,
                        CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }
Pankaj
  • 7,908
  • 6
  • 42
  • 65
-1

Can u replace this code

Uri imageUri = getPickImageResultUri(data);
Log.d("image picker", imageUri.toString());
mCropImageView.setImageUri(imageUri);

with.. This,

Uri imageUri = data.getData();
path = GetPathFromURI(context, imageUri);
bitmap  = BitmapFactory.decodeFile(path);
mCropImageView.setImageBitmap(bitmap);

GetPathFromURI

public static String GetPathFromURI(Context context,Uri contentUri) {
        String realPath = "";
        String[] projection = { MediaStore.Images.Media.DATA };
        ContentResolver cr = context.getContentResolver();
        Cursor cursor = cr.query(contentUri, projection, null, null, null);
        if (cursor.moveToFirst()) {
            realPath = cursor.getString(0);
        }
        cursor.close();
        return realPath;
    }
SHIDHIN TS
  • 1,557
  • 3
  • 26
  • 58
  • The problem is not about cropping. The problem is that the image is not being loaded from the camera. Cropping happens properly. – Pratik Borole Jun 22 '15 at 07:20
  • @PratikBorole can u please check this – SHIDHIN TS Jun 22 '15 at 07:26
  • So turns out, the `Intent data` from `onActivityResult` is null. Because i get `Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference` message in log. What do i do? – Pratik Borole Jun 22 '15 at 08:43