2

我正在创建一个应用程序,在应用程序启动相机打开拍照。我还在应用程序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);
}

} 提前致谢。

4

1 回答 1

2

好吧,实际上根本不调用 your 是很正常的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);
}
于 2013-02-27T18:34:15.783 回答