繁体   English   中英

OnActivityResult不响应DialogFragment

[英]OnActivityResult Doesn't respond to DialogFragment

我有一个DialogFragment,它弹出来,要求用户选择拍照,或者从图库中选择一个。 当我打开图库并选择图像时,什么也没有发生,并且OnActivityResult不响应。 是否是因为DialogFragment类在选择图像后不久就关闭了,并且没有时间通过​​OnActivityResult类?

这是DialogFragment类的onCreateDialog:

public class PhotoFragment extends DialogFragment {


public  static final int SELECT_PHOTO = 100;

public Bitmap image;


public static interface OnCompleteListener {
    public abstract void onComplete(Bitmap image);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setItems(new CharSequence[]
                    {"Take a Photo", "Select Image From Gallery"},
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // The 'which' argument contains the index position
                    // of the selected item
                    switch (which) {
                        case 0:
                            Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            startActivityForResult(takePicture, 0);//zero can be replaced with any action code
                            break;
                        case 1:
                            doToast("Picking Image",true);
                            Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            startActivityForResult(pickPhoto , SELECT_PHOTO);//one can be replaced with any action code
                            break;

                    }
                }
            });
     return builder.create();
}

这是activityOnResult()的代码(位于同一类PhotoFragment.java中)

@Override
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    onActivityResult(requestCode, resultCode, imageReturnedIntent);

    doToast("Activity Result", true);
    switch (requestCode) {
        case SELECT_PHOTO:

                Uri selectedImage = imageReturnedIntent.getData();
            InputStream imageStream = null;
            try {
                imageStream = getContext().getContentResolver().openInputStream(imageReturnedIntent.getData());
            } catch (FileNotFoundException e) {
                doToast("Input Stream not found", true);
                e.printStackTrace();
            }
            Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
            image = yourSelectedImage;


    }
}

确保您的活动中没有onActivityResult。

在您的活动中添加onActivityResult,然后获取片段并调用片段onActivityResult。 例:

  @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
    fragment.onActivityResult(requestCode, resultCode, data);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM