在多图像选择器中OnActivityResult()方法未在Fragment中调用

时间:2017-09-25 08:49:41

标签: android android-fragments android-intent onactivityresult android-image-capture

我正在尝试为应用程序制作图像选择器。因为我使用的是多图像选择器库,当在活动中使用时它是完美的,但是在这里我想在片段中使用它。片段OnActivityResult()方法是不是被人打电话。任何人都可以帮我解决这个问题吗?

这是我的代码: MainActivity.java :(我的主要活动)

public class Image extends Fragment {
    private ArrayList<String> mSelectPath;
    private static final int REQUEST_IMAGE = 2;
    ArrayList<Uri> mMedia = new ArrayList<Uri>();
    Uri uri;
    ImageView img;
    protected static final int REQUEST_STORAGE_READ_ACCESS_PERMISSION = 101;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_image, container, false);
        img = (ImageView) view.findViewById(R.id.img);
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pickImage();
            }
        });

        return view;
    }
    public void pickImage() {
            MultiImageSelector selector = new MultiImageSelector(getActivity());
            selector.showCamera(true);
            selector.multi();
            selector.count(1);
            selector.origin(mSelectPath);
            selector.start(getActivity(), REQUEST_IMAGE);
        }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE) {
            if (resultCode == getActivity().RESULT_OK) {
                mSelectPath = data.getStringArrayListExtra(MultiImageSelector.EXTRA_RESULT);
                mMedia.clear();
                for (String p : mSelectPath) {
                    Uri uri = Uri.parse(p);
                    mMedia.add(uri);
                }
                uri = mMedia.get(0);
                Log.e("uri", "  " + uri);
                if (!uri.toString().contains("content://")) {
                    uri = Uri.fromFile(new File(uri.toString()));
                    Log.e("in if", " uri = " + uri);
                }
                try {
                    Glide.with(this)
                            .load(uri)
                            .placeholder(R.mipmap.ic_launcher)
                            .error(R.mipmap.ic_launcher)
                            .into(img);
                } catch (Exception e) {
                    Log.e("Exceptionn", " " + e);
                }
            }
        }
    }

图片:(我的片段)

T1: // first need to set a proper timestamp and FinishDate
Load *, timestamp(Timestamp+[Duration (Hours)]/24) as FinishDate;
Load *, timestamp(Timestamp#(left(Date,19),'DD-MM-YYYY hh:mm:ss')) as Timestamp;
LOAD * INLINE [
    Variable, Status, Date, Duration (Hours)
    A, StatusA, 10-09-2017 18:30:00.00, 4
    A, StatusB, 10-09-2017 23:30:00.00, 5
    B, StatusA, 10-09-2017 10:00:00.00, 8
    B, StatusB, 10-09-2017 21:45:00.00, 9
];

left join // add a split-flag where needed
Load Distinct Timestamp,FinishDate,fabs(Date(left(FinishDate,10))-Date(left(Timestamp,10)) >=1) as SplitFlag
Resident T1;

T2: // load first part (current day) of split-flag=1
Load Variable,Status,
	 Timestamp,
	 dayEnd(Timestamp) as FinishDate,
	 round((DayEnd(Timestamp)-Timestamp)*24,0.1) as [Duration (Hours)]
Resident T1
where SplitFlag=1;
Concatenate // load second part (next day) where split-flag=1
Load Variable,Status,
	 daystart(FinishDate) as Timestamp,
	 FinishDate,
	 round((FinishDate-daystart(FinishDate))*24,0.1) as [Duration (Hours)]
Resident T1
where SplitFlag=1;
Concatenate // add the rest of the data (split-flag=0)
Load * Resident T1 Where SplitFlag=0;

drop table T1;	 

4 个答案:

答案 0 :(得分:3)

在您的活动中尝试此操作

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d("Parentactivity", "onActivityResult");

    if (requestCode == REQUEST_IMAGE) { //use request code as REQUEST_IMAGE while starting intent for camera

        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frame);
        fragment.onActivityResult(requestCode, resultCode, data);//calling fragments onActivityResult here
    }
}

答案 1 :(得分:0)

IIRC onActivityResult没有在片段上调用,它会在您的父级Activity上调用。您需要在Activity中捕获它并将结果转发到片段。

答案 2 :(得分:0)

你必须有意地打电话

public void pickImage() {
       Intent intent = new Intent(getContext(), MultiImageSelectorActivity.class);
       intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
       intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 1);
       intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_MULTI);
       intent.putStringArrayListExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, mSelectPath);
       startActivityForResult(intent, REQUEST_IMAGE);
}

如果你在做getActivity().startActivityForResult(intent, REQUEST_IMAGE); on OnActivityResult()将在活动而不是片段上发生。你使用它的方式就是这样,所以你需要使用意图方式。

也可能会使您的代码正常工作

public void pickImage() {
        MultiImageSelector selector = new MultiImageSelector(getContext());
        selector.showCamera(true);
        selector.multi();
        selector.count(1);
        selector.origin(mSelectPath);
        selector.start(getContext(), REQUEST_IMAGE);
}

答案 3 :(得分:0)

有两种方法看起来相同,但行为略有不同。 如果方法是从activity调用的,那么除非从fragment中调用,否则它不会给回调片段。

pypy setup.py develop --uninstall

如果有办法从片段中调用它,请查看库。

我不知道这个库,只是想象一下你是否可以传递片段 Activity.startActivityForResult() Fragment.startActivityForResult() MultiImageSelector selector = new MultiImageSelector(getActivity());

祝你好运

埃姆雷