onActivityResult问题

时间:2013-02-27 18:17:36

标签: android android-activity android-imageview

我正在创建一个应用程序,在应用程序启动相机打开拍照。我还在应用程序FB Login中实现了Facebook登录,一切正常。我的问题是我想要显示我在MainActivity布局中添加的imageView中拍摄的照片。没有错误,但imageView中没有显示图像。但是当我将onActivityResult方法的名称更改为onActivityResult2时,图像不会显示。当我不更改方法名称时显示图像。我无法将名称保留在onActivityResult上,因为在另一个类中,此方法正在处理Facebook登录。我不知道我做错了什么。谁能帮我吗。这是我的完整代码

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);
  }  
} 
}

以下是我的片段类

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);
}

} 在此先感谢。

1 个答案:

答案 0 :(得分:2)

嗯,实际上你的onActivityResult1()根本没有被调用是很正常的,因为start-for-result模式需要特定的函数签名才能正常工作。您所需要的只是:

  • MainActivity.onActivityResult1(...)

  • 中重命名MainActivity.onActivityResult(...)方法
  • 删除MainFragment.onActivityResult1(...)方法并使MainFragment.onActivityResult(...)看起来像这样:

@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);
}