如何使用savedInstanceState在方向更改时保存相机信息
我正在开发一款带有相机意图捕获图像的Android应用程序。我的应用程序不限于任何方向,当用户以纵向开始时,应用程序和应用程序都会崩溃,然后在横向上拍摄图像,然后在横向上按OK。我不想在我的Manifest中使用:android:configChanges =“orientation | keyboardHidden | screenSize”,因为我有不同的纵向和横向布局。我试图使用“savedInstanceState”,但我没有成功,因为我不知道我需要保存哪些数据以及哪里需要调用保存的实例。如何使用savedInstanceState在方向更改时保存相机信息
错误是: java.lang.RuntimeException:无法恢复活动{.MainActivity}:java.lang.RuntimeException:传递结果失败ResultInfo {who = null,request = 0,result = -1,data = null}到activity {.MainActivity}:java.lang.NullPointerException:
注意:捕获的图像将通过意图发送到另一个活动: 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);
这个问题的答案是由图像的保存和恢复开放的时候方向更改:
protected void onSaveInstanceState(Bundle outState)
{
outState.putParcelable("outputFileUri", outputFileUri);
}
if (savedInstanceState != null)
{
outputFileUri= savedInstanceState.getParcelable("outputFileUri");
}
答案在于s链接: Answer
保存在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()
恢复。
的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被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);
}
非常有帮助,但问题在于相机。如何保存数据以及我需要分配哪种类型的变量。 – Nani
您正在使用变量实例,该变量在旋转后变为null。使用前请检查是否为空。你的应用程序崩溃与NullPointerException不是吗? – greenapps
是的,它由空值引起的数据变为空,我不知道如何从相机保存数据。如果我使用configChanges = orientation,这将解决问题,但我不想使用它。 – Nani
我想知道为什么Igor删除他的评论,因为使用onSaveInstanceState()正是你应该做的。 – greenapps