onActivityResult 不从 viewPager 片段调用

onActivityResult doesn't call from viewPager fragment

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

这是我调用 startActivityForResult 的方式:

private void setAvatarDialog(){
        final CharSequence[] options = {"Choose from Gallery", "Take Photo" };

        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(title);
        builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int 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"), SELECT_FILE);
                } else if (options[which].equals(options[1])) {
                    mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(mIntent, REQUEST_CAMERA);
                }
                dialog.dismiss();
            }
        });
        builder.setNegativeButton(negative, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.show();
    }

这是我的 onActivityResult:

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

有什么想法吗?

您需要确保您开始的 activity 结果也是 returns 的结果。例如,在您的图像捕获 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

查看

都说ViewPager里面的Fragment有问题,我也有同样的问题

当我有一个包含 ViewPager 的 ParentFragment 和 3 个不同的片段 (children) 并且其中一个 children 片段启动了一个 activity 时,这个解决方案对我有用。

您可以用 getParentFragment().startActivityForResult

在 child 中打开 activity

一旦 activity 完成,它将在 ParentFragment 中调用 onActivityResult,我在其中重写方法来调用 3 children 的每个 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);
    }
}

这对我有用。

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