您好我在主片段中使用了带有fragmnets的viewPager。
我正在尝试从图库或摄像头获取图像到位图,但是在选择照片和startActivityForResult后它没有捕获到onActivityResult ...


这里是我如何调用startActivityForResult:


 private void setAvatarDialog(){
 final CharSequence [] options = {“从画廊中选择”,“拍照”};

 String title = getString(R.string.alertDialog_editProfile_updateAvatar_title);
 String negative = getString(R.string.alertDialog_editProfile_updateAvatar_negative);

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
 builder.setTitle(标题);
 builder.setItems(options,new DialogInterface.OnClickListener(){
 @Override
 public void onClick(DialogInterface dialog,int which which){
 if(options [which] .equals(options [0] )){
 mIntent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
 mIntent.setType(“image / *”);
 startActivityForResult(Intent.createChooser( mIntent,“选择文件”),SELECT_FILE);
} else if(options [which] .equals(options [1])){
 mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);&#xA ; startActivityForResult(mIntent,REQUEST_CAMERA);
}
 dialog.dismiss();
}
});
 builder.setNegativeButton(否定,新的DialogInterface.OnClickListener(){
 @Override
 public void onClick(DialogInterface对话框,int which which){
 dialog.dismiss();
}&# xA;});
 builder.show();
 }



 这是我的onActivityResult:


 @ Override& #xA; public void onActivityResult(int requestCode,int resultCode,Intent data){
 super.onActivityResult(requestCode,resultCode,data);
 if(resultCode == Activity.RESULT_OK){
 if(requestCode == REQUEST_CAMERA){
 resultCamera(数据);
 } else if(requestCode == SELECT_FILE){
 resultGallery(数据);
 }
 }
 



 请问任何想法?

答案 0 :(得分:13)
当我的ParentFragment包含一个包含3个不同片段(子节点)的ViewPager并且其中一个子片段启动了一个活动时,这个解决方案对我有用。
您可以在getParentFragment().startActivityForResult
一旦该活动完成,它将在ParentFragment中调用onActivityResult
,其中我覆盖该方法以调用3个孩子中的每一个onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
for (Fragment fragment : getChildFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
答案 1 :(得分:0)
您需要确保为结果启动的活动也会返回结果。例如,在图片捕获Activity
中,Activity
结束,包括
Intent intent = new Intent();
//intent.putExtra
intent.putExtra(getString(R.string.MY_RESULT_CODE), data);
setResult(RESULT_OK, intent);
finish(); //Ends the current activity.
//After this, onActivityResult of the activity that started this one will be called
答案 2 :(得分:0)
查看https://stackoverflow.com/a/24303360/3207339
所有表明它是一个问题,当ViewPager中的片段时,我有同样的问题。
答案 3 :(得分:0)
这对我有用。
// Parent Fragment
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
getTargetFragment().onActivityResult(requestCode, resultCode, data);
}
// In Child Fragment
public void pick() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Note followings two lines.
getParentFragment().startActivityForResult(Intent.createChooser(intent, "Select"), 1);
getParentFragment().setTargetFragment(this,1);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("DEBUG","ONE1");
if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
Log.d("DEBUG","TWO");
Uri uri = data.getData();
try {
Log.d("DEBUG","THREE");
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), uri);
// Log.d(TAG, String.valueOf(bitmap));
Log.d("DEBUG","FOUR");
ImageView imageView = (ImageView) yourLayoutView.findViewById(R.id.yourImageView);
Log.d("DEBUG","FIVE");
imageView.setImageBitmap(bitmap);
Log.d("DEBUG", "SIX");
} catch (Exception e) {
e.printStackTrace();
}
}
}