我在左侧使用导航栏的Android应用程序上工作。当我点击一个按钮时,我可以显示我的片段,在另一个按钮上我启动了相机。
我想拍照并在我的片段中显示它,但我有以下java运行时异常:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=file:///storage/emulated/0/photo.jpg }} to activity {com.example.fred.myApplication/com.example.fred.myApplication.MainActivity}:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
我的代码:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, FormFragment.OnFormFragmentInteractionListener {
private File mFichier;
private ImageView mImageView ;
// On déclare une constante dans la classe FirstClass
public final static int REQUEST_IMAGE_CAPTURE = 1 ;
public final static String EXTRA_PICS_URI = "com.example.fred.myapplication.PICS_URI";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
mImageView = (ImageView) findViewById(R.id.imageView3);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("onActivityResult()", "start");
Log.i("onActivityResult()", "requestCode : "+requestCode+" - Expected : " +REQUEST_IMAGE_CAPTURE);
Log.i("onActivityResult()", "resultCode : " + resultCode + " - Expected : " +RESULT_OK);
if (resultCode != Activity.RESULT_CANCELED) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
if(data != null){
Uri selectedImage = data.getData();
try {
Bitmap bitmap = getBitmapFromUri(selectedImage);
mImageView.setImageBitmap(bitmap);
Fragment fragment = new FormFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.relativeLayout,fragment).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}catch (IOException e){
Log.i("onActivityResult()",e.toString());
mImageView.setBackgroundColor(Color.BLUE);
}
}
}else {
super.onActivityResult(requestCode, resultCode, data);
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
Log.i("onActivityResult()", "end");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// L'endroit où sera enregistrée la photo
// Remarquez que mFichier est un attribut de ma classe
mFichier = new File(Environment.getExternalStorageDirectory(), "photo.jpg");
// On récupère ensuite l'URI associée au fichier
Uri fileUri = Uri.fromFile(mFichier);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Et on déclare qu'on veut que l'image soit enregistrée là où pointe l'URI
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// Enfin, on lance l'intent pour que l'application de photo se lance
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
} else if (id == R.id.nav_gallery) {
Fragment fragment = new FormFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.relativeLayout,fragment).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
return true;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
public void onFormFragmentInteraction(Uri uri) {
}
/**
*
* @param uri
* @return
* @throws IOException
*/
private Bitmap getBitmapFromUri(Uri uri) throws IOException {
ParcelFileDescriptor parcelFileDescriptor =
getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
return image;
}
}
答案 0 :(得分:3)
有两种选择。
选项1。
fragmentManager.commitAllowingStateLoss();
或
fragmentManager.commitNowAllowingStateLoss();
而不是fragmentManager.commit();
选项2。
new Handler().postDelayed(
new Runnable() {
//...
fragmentManager.commitAllowingStateLoss();// or fragmentManager.commit();
//...
}, 50);