-1

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);
Saurabh
  • 434
  • 1
  • 4
  • 12
Nani
  • 97
  • 13
  • You are using a variable instance that becomes null after rotation. Please check for null before use. Your app crashes with a NullPointerException isnt it? – greenapps Oct 20 '17 at 11:19
  • yes, it caused by null value where the data become null and I do not know how to save the data from camera. If i use the configChanges=orientation, this will solve the problem but i do not want to use this. – Nani Oct 20 '17 at 11:57
  • I wonder why Igor removed his comment as using onSaveInstanceState() is exactly what you should do. – greenapps Oct 20 '17 at 12:04
  • `Where the data becomes null`. Please be exact! Which variable is it? You should of course save the value of that variable in mentioned function. – greenapps Oct 20 '17 at 12:06
  • when i change orientation when the camera device is working and press ok to save the image. – Nani Oct 20 '17 at 12:24
  • `I do not know how to save the data from camera`. You do not have to save data from camera. You should save your own data. You should save the value of the variable which becomes null. I wonder why you still did not inform us which variable that is. – greenapps Oct 20 '17 at 12:47
  • I am new to Android and I am trying to catch up. I tried to explain above that the data is null: ResultInfo{who=null, request=0, result=-1, data=null} – Nani Oct 20 '17 at 13:23
  • It is long time you show the used code. Why didnt you? – greenapps Oct 20 '17 at 13:40
  • `if (!exportDir.exists()) { exportDir.mkdirs(); } else { exportDir.delete(); }` ??? Why are you deleting a directory where you want to put a file in? Further you should check the return value of mkdirs() as it might fail to create the dir. In that case do not continue but display a toast to the user and return. – greenapps Oct 20 '17 at 15:08
  • And whete is the code that provokes the nullpointerexception? – greenapps Oct 20 '17 at 15:11
  • That is a nonsense remark. – greenapps Oct 20 '17 at 15:28

2 Answers2

1

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);
}
toha
  • 5,095
  • 4
  • 40
  • 52
Ivan Bošnjaković
  • 401
  • 1
  • 3
  • 12
  • Very helpful but the problem is with the camera. How to save the data and what type of variable i need to assign to this. – Nani Oct 20 '17 at 12:00
0

The answer to this question is by saving and restoring the Uri of the image when the orientation changes:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelable("outputFileUri", outputFileUri);
}
if(savedInstanceState != null) {
    outputFileUri= savedInstanceState.getParcelable("outputFileUri");
}

Answer is here: Answer

Nelson Katale
  • 1,309
  • 15
  • 21
Nani
  • 97
  • 13