0

我正在尝试为应用程序制作图像选择器。为此,我正在使用多图像选择器库,该库在活动中使用时非常有效,但在这里我想在片段中使用它。所以片段 OnActivityResult() 方法没有得到打电话。谁能帮我解决这个问题?

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

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_IMAGE = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getFragmentManager().beginTransaction().replace(R.id.frame, new Image_Selecter()).commit();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //  Utilz.printLog("Parentactivity", "onActivityResult");
        Log.e("hererererer", "hererererer");
        if (requestCode == REQUEST_IMAGE) { 

          new Image().onActivityResult(requestCode, resultCode, data);
        }
    }
}

图片:(我的片段)

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);
                }
            }
        }
    }
4

4 回答 4

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
    }
}
于 2017-09-25T08:53:08.870 回答
0

有两种方法看起来相同,但行为略有不同。如果方法被活动调用,它不会给片段回调,除非从片段调用。

Activity.startActivityForResult()
Fragment.startActivityForResult()

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

我不知道图书馆,只是成像如果你可以将片段传递给 MultiImageSelector selector = new MultiImageSelector(getActivity());selector.start(getActivity(), REQUEST_IMAGE);

祝你好运

埃姆雷

于 2017-09-25T08:56:19.847 回答
0

IIRConActivityResult不会在 Fragment 上调用,它会在您的父 Activity 上调用。您需要在 Activity 中捕获它并将结果转发到片段。

于 2017-09-25T08:53:30.717 回答
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); onOnActivityResult()将发生在活动而不是片段上。你使用它的方式就是这样,所以你需要使用intent的方式。

也可能会让你的代码工作

public void pickImage() {
        MultiImageSelector selector = new MultiImageSelector(getContext());
        selector.showCamera(true);
        selector.multi();
        selector.count(1);
        selector.origin(mSelectPath);
        selector.start(getContext(), REQUEST_IMAGE);
}
于 2017-09-25T08:55:01.673 回答