0

So I have a Fragment that calls the following method which launches the camera:

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

And I expect to receive the picture data in this method in my fragment:

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (!(resultCode == RESULT_OK)) return;

        if (requestCode == REQUEST_IMAGE_CAPTURE || requestCode == REQUEST_GALLERY_IMAGE) {
            Uri imageURI = data.getData();

// do something
    }
}

However, after I take a picture and confirm it, the app goes to my launcher. After setting breakpoints in the onActivityResult method, the app never even reaches this method before crashing. I've granted made sure to grant all permissions in both the manifest and at runtime.

There are also no outputs to Logcat with this crash, both in the app logs and the device logs. I have also tested on both my device (Moto G5 Plus) & Pixel XL API 26 emulator; both have the same result.

BeardMagician
  • 647
  • 13
  • 26
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Nov 02 '17 at 18:06
  • @CommonsWare see post update, there are no logcat outputs with this crash – BeardMagician Nov 02 '17 at 18:11
  • If something crashes, there is a stack trace in LogCat, almost by definition. If the camera app is crashing, the log messages would be tied to its process, not yours, and you would need to set the Logcat view's filters to accommodate that. – CommonsWare Nov 02 '17 at 18:36
  • @CommonsWare Like I said I checked the device logcat, there's nothing that shows up. And its not a normal crash where I get a "app stopped working" message, the camera just never returns to the activity – BeardMagician Nov 02 '17 at 20:08
  • What API level and total RAM of device? – Hack5 Nov 02 '17 at 20:49
  • @Penn API 25, RAM is 4gb. The same thing happens in emulator as well – BeardMagician Nov 02 '17 at 21:57

4 Answers4

0

I think you need to call super.onActivityResult(). The fragment is the one making the startActivityForResult() call, but the activity gets the first shot at handling the result so you need to implement super.onActivityResult() to make the fragment handle the result.

Suhafer
  • 99
  • 4
0

the app immediately crashes to my launcher

No, the camera app immediately brings up the launcher. Apparently, the developers of this camera app wrote it to bring up the home screen when the user is done taking the picture. This is a bug, of course, but there is little that you can do about it.

Use ACTION_IMAGE_CAPTURE when you would like a picture but do not mind if you do not get it, due to bugs in some of the hundreds of camera apps that you are integrating with. Otherwise, use a camera library (Fotoapparat, CameraKit-Android, etc.) to take the picture directly in your own app.

Also, and FWIW, ACTION_IMAGE_CAPTURE does not return a Uri, so your onActivityResult() code will not work anyway. Given your particular ACTION_IMAGE_CAPTURE Intent configuration, in onActivityResult(), data.getParcelableExtra("data") will return a Bitmap representing a thumbnail-sized image.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So you're saying that if I want to get the bitmap, the following dev guide just won't work and I should use a third party library? Also, both the camera app from the Pixel XL and Moto G5 Plus have the same response https://developer.android.com/training/camera/photobasics.html – BeardMagician Nov 02 '17 at 22:18
  • @BeardMagician: I am saying that `ACTION_IMAGE_CAPTURE` is unreliable. Perhaps your app can deal with that unreliability, telling the user to install some other camera app that you know works well with your app. Perhaps that would be unsuitable for your app. That is your decision to make. There are [lots of ways in which camera apps screw up `ACTION_IMAGE_CAPTURE`](https://commonsware.com/blog/2015/06/08/action-image-capture-fallacy.html), and so while many camera apps will be fine, there will be those that have bugs. – CommonsWare Nov 02 '17 at 22:23
  • Don't make assumptions without evidence.EDIT: I take that back, no logcats does suggest that. – Hack5 Dec 17 '17 at 08:56
0

I believe this is yet another manifestation of the Android: Activity getting Destroyed after calling Camera Intent.

The bottom line is, the system restarts your app from scratch and your activity is created but has a chance to restore its state.

So you must implement onRestoreInstanceState(). I am not sure you can guarantee that the fragment will be ready to receive onActiviyResult() timely, so to be in the safe side, I prefer to handle the captured image in activity itself.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • I did this and added breakpoints to the onRestore methods (in both fragment & activity) - they were never reached – BeardMagician Nov 02 '17 at 22:21
  • Breakpoints won't work in this scenario, because if another instance of your app is launched by the system, debugger is not attached to it. Try to add logs to onCreate and onResume, and make sure that logcat does not filter by process id. – Alex Cohn Nov 03 '17 at 04:40
0

So this issue was actually because of an intent flag I had attached to the activity. My activity was started using the NO_HISTORY intent flag, which apparently prevented it from being recreated when returned from a startActivityForResult call.

BeardMagician
  • 647
  • 13
  • 26