我正在开发一款带摄像头意图捕获图像的Android应用程序。我的应用程序不限于任何方向,并且应该同时工作,当用户以纵向开始我的应用程序崩溃,然后以横向拍摄图像并按下风景中的确定。我不想在我的Manifest中使用: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:
注意:捕获的图像将通过以下方式通过意图发送到另一个活动:
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()
中恢复它。
的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
}
...
}
在onStart()
之后调用onRestoreInstanceState,你可以在这里恢复值:
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:
protected void onSaveInstanceState(Bundle outState)
{
outState.putParcelable("outputFileUri", outputFileUri);
}
if (savedInstanceState != null)
{
outputFileUri= savedInstanceState.getParcelable("outputFileUri");
}
答案在这个链接:Answer