我正在片段中使用相机意图拍照。在某些设备上它可以完美运行,但在某些设备上,我的片段在拍照后会关闭。
这是我调用相机意图的代码:
Intent picIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
getActivity().startActivityForResult(picIntent, Constants.TAKE_PICTURE_SIGN_UP);
这是我的MainActivity onActivityResult:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == Constants.TAKE_PICTURE_SIGN_UP || requestCode == Constants.SELECT_PICTURE_SIGN_UP)
{
SignUpFragment frag = (SignUpFragment)getSupportFragmentManager().findFragmentByTag(Constants.SignUpFragmentTag);
if(frag != null && frag.isVisible())
{
frag.onActivityResult(requestCode, resultCode, data);
}
}
}
当我从画廊中选择图片时它工作正常,但是当我从相机拍摄照片时,碎片为空。是什么导致这里的问题以及如何解决它?
答案 0 :(得分:1)
我使用以下系列,它对我有用
Intent picIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp1.jpg");
Uri mImageCaptureUri = Uri.fromFile(f);
picIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
getActivity().startActivityForResult(picIntent, Constants.TAKE_PICTURE_SIGN_UP);
答案 1 :(得分:1)
除了@Mina Azhar answer之外, 当系统在低内存中杀死后台活动时你必须处理。
fragment.startActivityForResult(picIntent, Constants.TAKE_PICTURE_SIGN_UP);
您需要保存片段状态,
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
outState.putParcelable("cameraMediaOutputUri", mImageCaptureUri);
super.onSaveInstanceState(outState);
}
恢复片段状态 Fragment Life Cycle
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState !=null && savedInstanceState.containsKey("cameraMediaOutputUri"))
mImageCaptureUri = savedInstanceState.getParcelable("cameraMediaOutputUri");
}
答案 2 :(得分:0)
使用这样的方法确保附加并显示您的片段:
private MyFragmentA switchToFragmentA(){
FragmentManager fm = getSupportFragmentManager();
MyFragmentA fragment = (MyFragmentA) fm.findFragmentByTag(TAG_A);
if(fragment == null){
fragment = MyFragmentA.newInstance();
}
fm.beginTransaction().replace(R.id.fragment_container, fragment, TAG_A).commit();
return fragment;
}
然后像这样调整你的onActivityResult:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == Constants.TAKE_PICTURE_SIGN_UP || requestCode == Constants.SELECT_PICTURE_SIGN_UP){
switchToFragmentA().onActivityResult(requestCode, resultCode, data);
}
}
答案 3 :(得分:0)
您致电:getActivity().startActivityForResult(intent,111);
你应该致电:startActivityForResult(intent,111);
也可以尝试
// In your activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
答案 4 :(得分:0)
问题:摄像机活动是否会破坏您的活动和碎片。现在正确的解决方案是实现所有片段和活动的onSaveInstanceState
方法。
Last Resort快速解决方案:将活动设置为不会破坏方向更改和屏幕重新调整大小等。对于主活动的清单文件或具有该特定片段的活动添加这一行
<activity
android:name=".MainActivity_"
android:configChanges="orientation|keyboardHidden|screenSize"
</activity>
请在此处阅读android:configChanges