问题
I am creating an app, On app start camera opens to take pic. I have also implemented the Facebook login in the app FB Login, everything is working fine. My problem is I want to show the pic taken in an imageView which I have added in the MainActivity layout. There is no error but image is not showing in imageView. But when I change the name of onActivityResult method to onActivityResult2 image doesn't display. Image displays when I dont change method name. I can't keep the name to onActivityResult because in another class this method is handling the Facebook Login. I don't know what I am doing wrong. Can anyone help me out. Here is my complete code
public class MainActivity extends FragmentActivity {
private MainFragment mainFragment;
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
private Button btn_up, btn_new;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
mainFragment = new MainFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, mainFragment).commit();
} else {
// Or set the fragment from restored state info
mainFragment = (MainFragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imview);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
protected void onActivityResult1(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
And following is my Fragment Class
public class MainFragment extends Fragment {
private static final String TAG = "MainFragment";
private UiLifecycleHelper uiHelper;
public Bitmap photo;
private int RESULT_OK = 1;
private ImageView imageView;
private static final int CAMERA_REQUEST = 1888;
private Button btn_up, btn_new;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setFragment(this);
imageView = (ImageView) view.findViewById(R.id.imview);
btn_new = (Button) view.findViewById(R.id.btn_new);
btn_up = (Button) view.findViewById(R.id.btn_up);
btn_new.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
return view;
}
public void onActivityResult1(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
//LoginButton aButton
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
Session session = Session.getActiveSession();
if (session != null &&
(session.isOpened() || session.isClosed()) ) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
//authButton.setFragment(this);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
} Thanks in Advance.
回答1:
Well, actually it's quite normal your onActivityResult1()
isn't called at all, because the start-for-result pattern need that specific function signature in order to work properly. All you need is to:
rename back
MainActivity.onActivityResult1(...)
method inMainActivity.onActivityResult(...)
delete
MainFragment.onActivityResult1(...)
method and make theMainFragment.onActivityResult(...)
look like this:
.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
else
uiHelper.onActivityResult(requestCode, resultCode, data);
}
来源:https://stackoverflow.com/questions/15119330/issue-with-onactivityresult