onActivityResult没有调用和Fragment从图像选择器Android关闭自己

时间:2014-08-05 11:46:17

标签: android android-fragments

嗨,我知道有很多与此相关的问题,但我仍然无法找到正确的方法:(

我读了这个链接onActivityResult is not being called in Fragment,但我找不到解决方案。

              Intent intent = new Intent();
              intent.setType("image/*");
              intent.setAction(Intent.ACTION_GET_CONTENT);
              startActivityForResult(Intent.createChooser(intent, ""),
                                    PICK_IMAGE);


        public void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (resultCode != Activity.RESULT_CANCELED) {
                    if (requestCode == PICK_IMAGE && data.getData() != null) {
                        imagePath = getAbsolutePath(data.getData());

                        imageLoader.loadImage("file://" + imagePath,
                                new ImageLoadingListener() {

                                    @Override
                                    public void onLoadingStarted(String imageUri,
                                            View view) {

                                    }

                                    @Override
                                    public void onLoadingFailed(String imageUri,
                                            View view, FailReason failReason) {

                                    }

                                    @Override
                                    public void onLoadingComplete(String imageUri,
                                            View view, Bitmap loadedImage) {
                                        height = loadedImage.getHeight();
                                        width = loadedImage.getWidth();
                                        if (Utils
                                                .validateImageSize(height, width,
                                                        getActivity(),
                                                        "Image size must be in 480 (width) & 800 (height)    with PNG or JPG format"))
                                            imageLoader.displayImage("file://"
                                                    + imagePath, ivSubmitImgWord,
                                                    options);
                                    }

                                    @Override
                                    public void onLoadingCancelled(String imageUri,
                                            View view) {
                                    }
                                });

                    }
                }

            }

当我从画廊中选择图像时,有时候onActivityResult不是调用而且Fragment是关闭的。请任何人都可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

您需要删除片段中的super.onActivityResult(requestCode, resultCode, data);并覆盖

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {}

答案 1 :(得分:0)

检查您的活动清单上是否设置了noHistory=true。这不会调用onActivityResult