Alex Knight
Alex Knight

Reputation: 79

onActivityResult from camera gives null intent

so this was working lovely till yesterday when I upgraded my phone to 5.0.0 and tested the application out in a 5.0.0 emulator. Basically the purpose is to allow the user to take a photo, return it, open it to allow cropping then return it and set it as an imageview.

Everything worked fine before but now the intent in onActivityResult is null.

Here's code:

public void takeDisplayPicture(View view) {

    final String TAKE_DISPLAY_PICTURE = "Take Display Picture";

    Log.d(TAKE_DISPLAY_PICTURE, "Clicked");

    try {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAMERA_CAPTURE);

    } catch (ActivityNotFoundException e) {

        String msg = "Your device doesn't support a camera!";

        Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        toast.show();

    }

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    final String RETURN_DISPLAY_PICTURE = "Return Display Picture";

    Log.d(RETURN_DISPLAY_PICTURE, "Returned");

    if (resultCode == RESULT_OK && requestCode == CAMERA_CAPTURE) {

        picUri = data.getData();
        performCrop(picUri);

    } else if (requestCode == PIC_CROP) {

        Bundle extras = data.getExtras();
        bitmap = extras.getParcelable("data");
        displayPicture.setImageBitmap(bitmap);

    }

}

private void performCrop(Uri picUri) {

    try {

        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        cropIntent.setDataAndType(picUri, "image/*");
        cropIntent.putExtra("crop", "true");
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        cropIntent.putExtra("outputX", 256);
        cropIntent.putExtra("outputY", 256);
        cropIntent.putExtra("return-data", true);
        startActivityForResult(cropIntent, PIC_CROP);

    } catch (ActivityNotFoundException e) {

        String msg = "Your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        toast.show();

    }

}

Upvotes: 0

Views: 181

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006574

First, Android does not have a CROP Intent.

With respect to the null Uri, it is supposed to be null. Quoting the ACTION_IMAGE_CAPTURE documentation:

The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field.

You do not have EXTRA_OUTPUT, and so you are supposed to get your image via the poorly-documented (Bitmap)data.getExtras().get("data");.

Upvotes: 1

Related Questions