Dowemo


Question:

I am developing an Android app with camera intent to capture images. My app is not restricted to any orientation and should work on both and my app crashes when user start in portrait and then take the image in landscape and press ok in the landscape. I do not want to use the: android:configChanges="orientation|keyboardHidden|screenSize" in my Manifest as I have different layout for portrait and landscape orientation. I tried to use the "savedInstanceState" but i did not succeed as I do not know which data i need to save and where i need to call the saved instances.

The error is: java.lang.RuntimeException: Unable to resume activity {.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {.MainActivity}: java.lang.NullPointerException:

Note: the captured image will be send to another activity through intent in the: protected void onActivityResult(int requestCode, int resultCode, Intent data) { ... }

Here is my code:

takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


                    if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {


                        File exportDir = new File(Environment.getExternalStorageDirectory(), "TempFolder");


                        if (!exportDir.exists()) {


                            exportDir.mkdirs();


                        } else {


                            exportDir.delete();


                        }


                        mTempCameraPhotoFile = new File(exportDir, "/" + UUID.randomUUID().toString().replaceAll("-", "") + ".jpg");


                        Log.d(TAG, "path to file: " + "/" + UUID.randomUUID().toString().replaceAll("-", "") + ".jpg");


                        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempCameraPhotoFile));


                        startActivityForResult(takePictureIntent, REQUEST_CAMERA);


                    }



onActivityResult



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



                Intent intent = new Intent(MainActivity.this, CameraActivity.class);


                //intent.putExtra("ID_EXTRA", new String[] { bytes.toByteArray(), "Load_Image"});


                intent.putExtra("imageUri", Uri.fromFile(mTempCameraPhotoFile));



                //intent.putExtra("imageUri", outputFileUri);


                intent.putExtra("Title", "Take_Image");


                startActivity(intent);



Best Answer:


save state in onSavedInstanceState() like provided below:

static final String STATE_SCORE = "playerScore";


static final String STATE_LEVEL = "playerLevel";


...



@Override


public void onSaveInstanceState(Bundle savedInstanceState) {


    // Save the user's current game state


    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);


    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);



    // Always call the superclass so it can save the view hierarchy state


    super.onSaveInstanceState(savedInstanceState);


}


and then restore it in onCreate() or in onRestoreInstanceState().

onCreate:

    @Override


protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState); // Always call the superclass first



    // Check whether we're recreating a previously destroyed instance


    if (savedInstanceState != null) {


        // Restore value of members from saved state


        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);


        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);


    } else {


        // Probably initialize members with default values for a new instance


    }


    ...


}


onRestoreInstanceState is called after onStart() and you can restore values here:

public void onRestoreInstanceState(Bundle savedInstanceState) {


// Always call the superclass so it can restore the view hierarchy


super.onRestoreInstanceState(savedInstanceState);



// Restore state members from saved instance


mCurrentScore = savedInstanceState.getInt(STATE_SCORE);


mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);


}





Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs