0

I am trying to make image selector for application.For that i am using multi-image-selector library which is perfeclty works when used in activity ,but here i want to use it in fragment.so in fragment OnActivityResult() method is not getting called.Can anyone help me to solve this?

Here's my code: MainActivity.java:(My Main Activity)

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

Image:(My Fragment)

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);
                }
            }
        }
    }
Chirag
  • 27
  • 7

4 Answers4

3

Try this in your activity

@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
    }
}
gaurang
  • 2,217
  • 2
  • 22
  • 44
  • but there is an option to get the result on the fragment, no need to go back to the activity – gmetax Sep 25 '17 at 08:56
  • why not use the startActivityForResult from the fragment instead? – gmetax Sep 25 '17 at 09:06
  • we have to use startActivityForResult from the fragment then we can get result in onActivityResult() – gaurang Sep 25 '17 at 09:07
  • @chirag your RequestCode isn't 11 or 111 that @gaurang has, it is 2 `REQUEST_IMAGE = 2` – gmetax Sep 25 '17 at 09:18
  • can you post your error log? where your are getting null pointer exception – gaurang Sep 25 '17 at 10:12
  • the code of @gaurang assumes that you have a fragment container `fragment_container`, so if you ( @Chirag) go with that code you have to change it to work on your scenario. – gmetax Sep 25 '17 at 10:15
  • @gmetax- I have used frame layout in main activity and inflated layout from fragment. – Chirag Sep 25 '17 at 10:21
  • fragment_container frame layout which is containing my fragment, it not necessary that questioner and my container are same. – gaurang Sep 25 '17 at 10:22
  • because I believe that @Chirag is new on android I wanted to point out that also maybe he didn't check that – gmetax Sep 25 '17 at 10:28
  • @gmetax-Yeah i am absolute beginner in android.I have added my main activity code above check if u find something wrong .thank you. – Chirag Sep 25 '17 at 10:56
0

IIRC onActivityResult doesn't get called on a Fragment, it gets called on your parent Activity. You would need to catch this in the Activity and forward the result on to the fragment.

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • you are wrong... the OnActivityResult can get called on Fragment! https://stackoverflow.com/a/6147919/2401265 – gmetax Sep 25 '17 at 08:59
  • Fair enough https://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment – Blundell Sep 25 '17 at 09:01
0

you have to call it with intent

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

if you were doing getActivity().startActivityForResult(intent, REQUEST_IMAGE); the on OnActivityResult() will happen on activity instead of fragment. The way you are using it is like that, so you need to use the intent way.

also that maybe will make your code to work

public void pickImage() {
        MultiImageSelector selector = new MultiImageSelector(getContext());
        selector.showCamera(true);
        selector.multi();
        selector.count(1);
        selector.origin(mSelectPath);
        selector.start(getContext(), REQUEST_IMAGE);
}
gmetax
  • 3,853
  • 2
  • 31
  • 45
0

There are two methods which looks same but behaves little bit different. If method is called by from activity, its not going to give callback to fragment unless called from fragment.

Activity.startActivityForResult()
Fragment.startActivityForResult()

Check out the library if there is way to calling it from fragment.

I dont know the library, just imaging if you can pass fragment to MultiImageSelector selector = new MultiImageSelector(getActivity()); or selector.start(getActivity(), REQUEST_IMAGE);

Good luck

Emre

Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30