Android Tablet Take Picture Camera Intent Activity Restarts

Khawar Raza

In my app, there is an option to take pictures from camera. My FragmentActivity runs in portrait mode only. In my Fragment, when user taps on button, I launches camera app using:

private void pickFromCamera() {

    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    try {
        File file = new File(createImageFile());
        if(!file.exists()) {
            file.createNewFile();
        }
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    } catch (Exception e) {
        e.printStackTrace();
    }
    startActivityForResult( intent , ACTION_REQUEST_CAMERA );

}

and in onActivityResult of fragment, I am doing this:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    try {
        super.onActivityResult(requestCode, resultCode, data);
        if ( resultCode == Activity.RESULT_OK ) {
            switch ( requestCode ) {
            case ACTION_REQUEST_GALLERY:
                .....
            case ACTION_REQUEST_CAMERA:

                if(cameraImageFilePath == null) {
                // show error message and return
                }
                addImage(cameraImageFilePath);
                break;

            }
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

but I am facing strange issue here. On phones, it works normally. On tablet it works when picture is taken in landscape mode and save button is tapped from camera app. But If I take picture in portrait and presses save button, my app crashed. Here is logcat output:

    09-18 17:00:09.960: E/AndroidRuntime(13701): FATAL EXCEPTION: main
09-18 17:00:09.960: E/AndroidRuntime(13701): java.lang.RuntimeException: Unable to resume activity {com.dstudios.napa/com.dstudios.napa.HomeActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=196708, result=-1, data=null} to activity {com.dstudios.napa/com.dstudios.napa.HomeActivity}: java.lang.NullPointerException
09-18 17:00:09.960: E/AndroidRuntime(13701):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2291)
09-18 17:00:09.960: E/AndroidRuntime(13701):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2319)
09-18 17:00:09.960: E/AndroidRuntime(13701):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1839)
09-18 17:00:09.960: E/AndroidRuntime(13701):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3191)
09-18 17:00:09.960: E/AndroidRuntime(13701):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-18 17:00:09.960: E/AndroidRuntime(13701):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1031)
09-18 17:00:09.960: E/AndroidRuntime(13701):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 17:00:09.960: E/AndroidRuntime(13701):    at android.os.Looper.loop(Looper.java:132)
09-18 17:00:09.960: E/AndroidRuntime(13701):    at android.app.ActivityThread.main(ActivityThread.java:4126)
09-18 17:00:09.960: E/AndroidRuntime(13701):    at java.lang.reflect.Method.invokeNative(Native Method)
09-18 17:00:09.960: E/AndroidRuntime(13701):    at java.lang.reflect.Method.invoke(Method.java:491)
09-18 17:00:09.960: E/AndroidRuntime(13701):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
09-18 17:00:09.960: E/AndroidRuntime(13701):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
09-18 17:00:09.960: E/AndroidRuntime(13701):    at dalvik.system.NativeStart.main(Native Method)
09-18 17:00:09.960: E/AndroidRuntime(13701): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=196708, result=-1, data=null} to activity {com.dstudios.napa/com.dstudios.napa.HomeActivity}: java.lang.NullPointerException
09-18 17:00:09.960: E/AndroidRuntime(13701):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2821)
09-18 17:00:09.960: E/AndroidRuntime(13701):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2278)
09-18 17:00:09.960: E/AndroidRuntime(13701):    ... 13 more
09-18 17:00:09.960: E/AndroidRuntime(13701): Caused by: java.lang.NullPointerException
09-18 17:00:09.960: E/AndroidRuntime(13701):    at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:152)
09-18 17:00:09.960: E/AndroidRuntime(13701):    at android.app.Activity.dispatchActivityResult(Activity.java:4581)
09-18 17:00:09.960: E/AndroidRuntime(13701):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2817)

Then I added onActivityResult method in FragmentActivity and surrounded the super call with try catch block as:

@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
    // TODO Auto-generated method stub
    try {
        super.onActivityResult(arg0, arg1, arg2);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Now what is happening is exception is catched and no crash but my activity is being restarted. I know there is a way to save the state of the activity using onSavedInstanceState and then... but my activity is very complex , it cannot be restored to its previous state bcz it is the main activity and a lot fragments get loaded and etc... Is there any workaround for this? I am using Samsung's 10" tablet having Android HoneyComb 3.2. Edit: Also, I have added android:configChanges="orientation" in manifest.

Alex Cohn

No, there is no way you can guarantee that your activity will not be destroyed as soon as it lost the screen. It does happen more often when the orientation is switched due to the Camera app requesting Landscape, but you have no command of the situation. The user can choose to run one more background service, or use a custom IMAGE_CAPTURE provider, which will burn all your cards. See also https://stackoverflow.com/a/20783313/192373.

If you cannot go for the onSaveInstanceState(Bundle) (or even if you can), it may be a much better experience for your users, if you provide an in-app camera, especially on a tablet with fragments.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ANDROID STUDIO: Take picture with Camera API -> Send this picture to another activity

From Dev

Android: take camera picture intent remove confirmation dialog

From Dev

Take picture only with android camera intent in Kotlin, not video

From Dev

Android - take picture - pass uri to camera app

From Dev

Android using front facing camera to take a picture

From Dev

Android - take picture - pass uri to camera app

From Dev

Take picture with front camera in android service

From Dev

android camera intent use another activity

From Dev

Android Camera2 : can't take picture with front camera

From Dev

Camera Intent destroys Activity

From Dev

Android- taking a picture for temporary use with the camera intent

From Dev

How to take a picture without an Intent and without any view window in Android

From Dev

How to take a picture without an Intent and without any view window in Android

From Dev

How to intent from activity to fragment picture ( android studio)

From Dev

Sent camera picture via intent

From Dev

Can I take Black and White pictures using android camera intent?

From Dev

Can I take Black and White pictures using android camera intent?

From Dev

Android : Take a photo from with in my layout without starting a camera intent

From Dev

Take a picture on a specific tablet crashes the application

From Dev

Camera Intent not returning to calling Activity

From Dev

Camera Intent vs Activity/Fragment

From Dev

Camera Intent not returning to calling Activity

From Dev

android: camera is not taking a picture

From Dev

why while calling camera in android all varibles(got from Activity1 through intent ) in the activity are reinitilized?

From Dev

fileNotFoundException camera intent - Android

From Dev

Taking a picture with a camera intent and saving it to a file

From Dev

How to take picture using Intent and send it by email

From Dev

How to use flash to take picture on custom camera?

From Dev

Take a camera picture and send it to php server in iPhone

Related Related

  1. 1

    ANDROID STUDIO: Take picture with Camera API -> Send this picture to another activity

  2. 2

    Android: take camera picture intent remove confirmation dialog

  3. 3

    Take picture only with android camera intent in Kotlin, not video

  4. 4

    Android - take picture - pass uri to camera app

  5. 5

    Android using front facing camera to take a picture

  6. 6

    Android - take picture - pass uri to camera app

  7. 7

    Take picture with front camera in android service

  8. 8

    android camera intent use another activity

  9. 9

    Android Camera2 : can't take picture with front camera

  10. 10

    Camera Intent destroys Activity

  11. 11

    Android- taking a picture for temporary use with the camera intent

  12. 12

    How to take a picture without an Intent and without any view window in Android

  13. 13

    How to take a picture without an Intent and without any view window in Android

  14. 14

    How to intent from activity to fragment picture ( android studio)

  15. 15

    Sent camera picture via intent

  16. 16

    Can I take Black and White pictures using android camera intent?

  17. 17

    Can I take Black and White pictures using android camera intent?

  18. 18

    Android : Take a photo from with in my layout without starting a camera intent

  19. 19

    Take a picture on a specific tablet crashes the application

  20. 20

    Camera Intent not returning to calling Activity

  21. 21

    Camera Intent vs Activity/Fragment

  22. 22

    Camera Intent not returning to calling Activity

  23. 23

    android: camera is not taking a picture

  24. 24

    why while calling camera in android all varibles(got from Activity1 through intent ) in the activity are reinitilized?

  25. 25

    fileNotFoundException camera intent - Android

  26. 26

    Taking a picture with a camera intent and saving it to a file

  27. 27

    How to take picture using Intent and send it by email

  28. 28

    How to use flash to take picture on custom camera?

  29. 29

    Take a camera picture and send it to php server in iPhone

HotTag

Archive