发生方向变化时如何使用savedInstanceState保存相机信息

how to use savedInstanceState to save camera information when the orientation change occure

我正在开发一个 Android 应用程序,该应用程序具有用于捕获图像的相机意图。我的应用程序不限于任何方向,应该可以在这两个方向上运行,并且当用户以纵向模式启动然后以横向模式拍摄图像并在横向模式下按 OK 时,我的应用程序崩溃了。我不想在我的清单中使用:android:configChanges="orientation|keyboardHidden|screenSize",因为我有不同的纵向和横向布局。我尝试使用 "savedInstanceState" 但我没有成功,因为我不知道我需要保存哪些数据以及我需要在哪里调用保存的实例。

错误是:

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: 

注意:捕获的图像将通过意图发送到另一个activity:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    ...
}

这是我的代码:

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);

onSavedInstanceState() 中保存状态,如下所示:

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);
}

然后在 onCreate()onRestoreInstanceState() 中恢复它。

创建时:

    @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 在 onStart() 之后调用,您可以在此处恢复值:

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);
}

这个问题的答案是在方向改变时保存和恢复图片的Uri:

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

答案在这里: Answer