0

In my application I take photo from the camera and display in an ImageView. This works perfectly fine when in portrait mode but when I use the app in landscape mode after taking the photo from the camera the moment the "Done" Button of camera is clicked. The app crashes and I get RunTimeException. My layout files for both portrait and landscape are similar. I am not able to solve this problem and I need some advise. Please help me asap.

Error in Logcat is shown below:

 11-19 14:35:19.404: E/AndroidRuntime(17122): FATAL EXCEPTION: main

11-19 14:35:19.404: E/AndroidRuntime(17122): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2500, result=-1, data=null} to activity {com.sample.postcare2/com.sample.postcare2.HowItWorksActivity}: java.lang.NullPointerException

11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3387)
11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3437)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.app.ActivityThread.access$1100(ActivityThread.java:139)
11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1291)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.os.Handler.dispatchMessage(Handler.java:99)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.os.Looper.loop(Looper.java:154)
11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.app.ActivityThread.main(ActivityThread.java:4944)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at java.lang.reflect.Method.invokeNative(Native Method)
11-19 14:35:19.404: E/AndroidRuntime(17122):    at java.lang.reflect.Method.invoke(Method.java:511)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at dalvik.system.NativeStart.main(Native Method)

11-19 14:35:19.404: E/AndroidRuntime(17122): Caused by: java.lang.NullPointerException

11-19 14:35:19.404: E/AndroidRuntime(17122):    at com.sample.postcare2.HowItWorksActivity.onActivityResult(HowItWorksActivity.java:218)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.app.Activity.dispatchActivityResult(Activity.java:4740)

11-19 14:35:19.404: E/AndroidRuntime(17122):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3383)
11-19 14:35:19.404: E/AndroidRuntime(17122):    ... 11 more

Code is shown below:

public class HowItWorksActivity extends Activity {

ImageButton btn_Account,btn_Photo,btn_Edit,btn_Flip,btn_Post;
RelativeLayout rl;
private static final int CAMERA_PIC_REQUEST = 2500;
private static final int SELECT_PICTURE = 1;
Bitmap bmap_image;
Uri uri_image;

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.activity_how_it_works);
    addListenerOnButton();
          }
   private void addListenerOnButton() {
      btn_Photo = (ImageButton) findViewById(R.id.btn_Photo);
      btn_Photo.setOnClickListener(new View.OnClickListener() {

//2 options here: 1 is to take picture and the other one is choose from gallery..Code for taking picture is below..

  private void takepicture() {
    ContentValues values = new ContentValues();
values.put(Media.TITLE,"PostCard Image");
values.put(Media.DESCRIPTION, "Image captured by camera via PostCare");
uri_image = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri_image);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}

    @SuppressWarnings("deprecation")
protected void onActivityResult(int requestCode,int resultCode,Intent data){
       if(requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK){
        //image = (Bitmap) data.getExtras().get("data");
        //bmap_image = image;                   
        imgview.setImageURI(uri_image);
    }
         super.onActivityResult(requestCode, resultCode, data);     
}

   @Override
protected void onSaveInstanceState(Bundle outState) {
    if(uri_image!=null)
        outState.putParcelable("post_card", uri_image);                 
    super.onSaveInstanceState(outState);
}
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
user2688158
  • 407
  • 5
  • 17
  • An error on `line:218`. it seem you are getting `uri_image null`, check null uri_image before setImageURI. – RobinHood Nov 19 '13 at 07:02
  • My guess is that `imgview` is **null** in `onActivityResult()`. – Alex Cohn Nov 19 '13 at 18:10
  • Thanks for your response guys, but I don't get it. It does not give any exception when I take photo or select from gallery in portrait mode. But how come uri_image or imgview is null in landscape mode. I am a beginner in the area of app development. So may be I might have missed something important, but I am not able to find out what.Please help me solve this problem. – user2688158 Nov 20 '13 at 03:16
  • @user2688158 did you find any solution for this? – GreenROBO Mar 11 '16 at 05:55

1 Answers1

1

You should prevent changes in the life cycle in your own Activity

android:configChanges="keyboardHidden|orientation|screenLayout|screenSize"
Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
Mr.Bug
  • 11
  • 1