我在使用Fragment中的相机意图时遇到问题。我有一个包含片段的Activity,我有四个片段。在我的第三个片段中,我调用了相机Intent。拍照后,应用程序不会仅在第3个片段上恢复,但它也会出现第1个片段。你可以看到图片和代码。
感谢任何解决方案。
代码:
public class CreateAttachment extends Fragment{
private RelativeLayout btnBack, btnNext;
private LinearLayout btnAttachment, btnCapture;
private static final int CAMERA_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.create_property_attachment, container, false);
btnBack = (RelativeLayout) view.findViewById(R.id.attachment_btnBack);
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().popBackStack();
}
});
btnNext = (RelativeLayout) view.findViewById(R.id.attachment_btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextFragment();
}
});
btnAttachment = (LinearLayout) view.findViewById(R.id.btnAttachment);
btnAttachment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), MEDIA_TYPE_IMAGE);
}
});
btnCapture = (LinearLayout) view.findViewById(R.id.btnCameraAccess);
btnCapture.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK && requestCode == MEDIA_TYPE_IMAGE){
Toast.makeText(getActivity(),"Photo is Chosen",Toast.LENGTH_SHORT).show();
}
else if (requestCode == CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Toast.makeText(getActivity(),"Photo is Captured",Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:0)
试试这个
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
在OnActivityResult中删除super.onActivityResult(requestCode, resultCode, data);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
}
答案 1 :(得分:0)
您的问题可能出在您的添加/替换片段代码中。你的第一和第三个片段是重叠的,因为你可能没有隐藏(片段化)你的片段用第三个片段替换第一个片段。
答案 2 :(得分:0)
感谢您的所有答案。无论如何,我并不确切地知道错误是什么,但解决这个问题的方法是覆盖我的第3个片段上的Activity的这两个函数。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(getActivity(), "Work on onCreate when return into Fragment.", Toast.LENGTH_SHORT).show();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}