gpt4 book ai didi

java - 拍照后显示 fragment

转载 作者:太空宇宙 更新时间:2023-11-03 13:50:28 27 4
gpt4 key购买 nike

我在开发一个左侧有导航栏的 Android 应用程序。当我点击一个按钮时,我可以显示我的 fragment ,在另一个按钮上我启动相机。

我想拍照并将其显示在我的 fragment 中,但出现以下 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;
}
}

最佳答案

有两种选择。


选项 1。

fragmentManager.commitAllowingStateLoss();

fragmentManager.commitNowAllowingStateLoss();

代替 fragmentManager.commit();


选项 2。

new Handler().postDelayed(
new Runnable() {
//...
fragmentManager.commitAllowingStateLoss();// or fragmentManager.commit();
//...
}, 50);

关于java - 拍照后显示 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35719926/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com