0

I want the user to select an image from the image gallery and the app will go into another activity and load the image there. This is to open the file chooser.

 private void openFileChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    getActivity().startActivityForResult(intent, PICK_IMAGE_REQUEST);
}

This is what i have in my onActivityResult

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
            && data != null && data.getData() != null) {
        mImageUri = data.getData();
        Intent intent = new Intent(getActivity(),AddActivity.class);
        intent.putExtra("imagePath",mImageUri.toString());
        intent.putExtra("requestCode",PICK_IMAGE_REQUEST);
        startActivity(intent);
    }

This is my code for the second activity, basically the user can either open the camera and take a photo or choose from an image gallery. The taking photo from camera works but the choosing image does not thats why i have two request codes.

 @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_additem);
    Log.i(TAG, "onCreate: started");
    setupBottomNavigationView();

    imageView = findViewById(R.id.addPic);

    Bundle extras = getIntent().getExtras();

    int requestCode = extras.getInt("requestCode");
    if (requestCode == 0) {
        if (extras != null) {
            Bitmap image = (Bitmap) extras.get("image");
            if (image != null) {
                imageView.setImageBitmap(image);
            }
        }
        if (requestCode == 1) {
            Intent intent = getIntent();
            String image_path = intent.getStringExtra("imagePath");
            Uri fileUri = Uri.parse(image_path);
            imageView.setImageURI(fileUri);
        }
    }
}

Been unable to understand why it does not work, my android app does not even go to the second activity after choosing the image. It just returns to the home activity.

Nipun Thennakoon
  • 3,586
  • 1
  • 18
  • 26
calveeen
  • 621
  • 2
  • 10
  • 28
  • Is this code (the `onActivityResult`) in an Activity or a Fragment? Could be similar to this issue https://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment The accepted answer there suggests calling `startActivityForResult` not `getActivity().startActivityForResult` – Tyler V Jul 01 '18 at 17:12
  • It is from a fragment. The taking photo works fine though – calveeen Jul 01 '18 at 23:58

3 Answers3

0

change

if (requestCode == 0) {

to

if (requestCode != 0) {
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
0

I'm not sure if this is cause of all the issues you noted, but your if-statements are nested when they probably shouldn't be

if(requestCode == 0 ) {
    // only gets here if requestCode == 0

    if(requestCode == 1) {
        //this will never be executed, requestCode == 0
    }
}

I think you want something more like

switch(requestCode) {
    case 0:
        // do stuff
        break;
    case 1:
        // do stuff
        break;
}

I assume PICK_IMAGE_REQUEST is set to 1, and this is the case that isn't working?

Tyler V
  • 9,694
  • 3
  • 26
  • 52
  • Hi i changed the code to the switch statement and it still didnt work though.The second activity is not even opened after i choose the image, startActivity is not even called in my fragment class. So that means the problem lies in my onActivityResult? – calveeen Jul 01 '18 at 16:55
  • Yeah, I don't think this is related to not launching the second activity. Have you verified that your code is being called in `onActivityResult`? – Tyler V Jul 01 '18 at 16:57
0

All right i solved it. I was calling activity's on result activity in the file chooser(). I amended that to just startActivityFor(...) instead of getActivity().startActivityFor(...)

calveeen
  • 621
  • 2
  • 10
  • 28