3

I have started testing my app on a Moto E2, which is one of the first Android Lollipop devices on the marked. Turns out I'm unexpectedly having trouble capturing images with the camera. I cannot receive a picture.

Creating an image capture intent using:

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

Upon returning to my activity the Intent contains no data, i.e. data.getData() returns null.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode != RESULT_OK) return;

    switch(requestCode) {
        case PICK_FROM_CAMERA:
        (...)
    }
}

On Moto E2 running Android 5.0.2: enter image description here

Now there is a flood of questions on SO here with similar issues and a variety of different causes. What really puzzles me here is that this code works just fine on my other Android devices running KitKat and Jelly Bean (see below). What could be the reason for this behavior, and how can I fix it?

On Galaxy S4 mini running Android 4.4.2: enter image description here

Abdul Aziz Barkat
  • 19,475
  • 3
  • 20
  • 33
  • `the Intent contains no data.`. What do you mean? `data==null` ? – greenapps Apr 21 '15 at 16:59
  • 1
    `Android 5.0` have some extra filtering to handle the `Intent`. Therefore, you might have to handle it this way. You can give it a try as it has been changed in `Camera API` for 5.0 https://developer.android.com/training/camera/photobasics.html#TaskCaptureIntent – Jibran Khan Apr 21 '15 at 17:03
  • use crop after clicking the image – Barun Apr 21 '15 at 17:14
  • `(...)` ... `this code works just fine on my other Android devices`. Please show your code. – greenapps Apr 21 '15 at 17:32
  • @greenapps: The image I was able to retrieve using `data.getData()`. As you can see that is null in case of the Moto E2. What code are you missing? Not sure if it helps adding what I'm doing with the image afterwards. That's simply missing the input. –  Apr 21 '15 at 17:40
  • @Barun: Yes, cropping is the next step. But I'm not sure what you mean. –  Apr 21 '15 at 17:43
  • `The image I was able to retrieve using data.getData()`. Well could you give more complete code?. On my Lolllipop device my code works. – greenapps Apr 21 '15 at 17:44
  • @greenapps: I updated the question with the remaining `onActivityResult()` code. Hope that it does not distract from the issue. There is another intent for cropping the image, taking `data.getData()` as input. That returns null, and the resultCode of the crop intent is RESULT_CANCELED. That's the whole story. –  Apr 21 '15 at 18:17
  • That is incomplete code as nowhere `croppedImageFile` in `Uri croppedImage = Uri.fromFile(croppedImageFile);` is declared/initiated/used.I really wonder what you mean by `The image I was able to retrieve using data.getData()' Tou arte NOT retrieving an image from `data.getData()`. You are handling PICK_FROM_CAMERA: and PICK_FROM_FILE in the same case. Mostly you have to handle them different. – greenapps Apr 21 '15 at 18:27
  • 1
    Ok. croppedImageFile is not that interesting i see now. You think data.getData() is a content path or a file path? Normally with ACTION_IMAGE_CAPTURE a thumbnail is returned by Intent data. `Bundle extras = data.getExtras(); Bitmap bitmap = (Bitmap) extras.get("data");`. – greenapps Apr 21 '15 at 18:31
  • 'As you can see that is null in case of the Moto E2.'. I did not see that anything was null. And what is it worth if it does contain a Bitmap? – greenapps Apr 22 '15 at 07:58
  • 4
    @jerry please don't add an answer to the question in the question itself, about the plagiarism you just need to edit your answer and properly put the code in blockquotes (Since you've copied them from the docs) and flag for moderators to undelete. – Abdul Aziz Barkat Oct 23 '22 at 17:43
  • 1
    You were, in fact, told what you need to do in your [meta question](https://meta.stackoverflow.com/q/421017/2029983), which you have since deleted after you said you followed the referencing guidelines; we can now validate our suspicious; you did not follow them as the content is not a block quote, which denotes that the content is not original and *quoted* from another (cited) source. – Thom A Oct 23 '22 at 18:11
  • 2
    As for the problem, mods are here to enforce things, not fix the content problems. You, however, can *easily* fix the problem by properly quoting the content and then mod flagging for undeleting. Plagiarising the content *again* is the completely wrong action. – Thom A Oct 23 '22 at 19:31
  • 1
    I haven't said anything about a moderator editting... Here or in your deleted [meta question](https://meta.stackoverflow.com/q/421017/2029983). The moderator deleted your content as it didn't follow the referencing guidelines, it's as simple as that. Correct the post to follow those guidelines and then it can be undeleted. – Thom A Oct 23 '22 at 19:33

1 Answers1

1

Android 5.0 has some extra filtering to handle the Intent. Therefore, you might have to handle it this way. You can give it a try as it has been changed in Camera API for 5.0

Part of it is here

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

More details can be found in the documentation for Android 5.0 API changes.

https://developer.android.com/training/camera-deprecated/photobasics#TaskPath

Abdul Aziz Barkat
  • 19,475
  • 3
  • 20
  • 33
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50