Search code examples
androidonactivityresult

onActivityResult with different intents?


I have an app where I want to use two buttons, one for opening the camera and the other should open the gallery and select a image.

What is the correct way to create the onActivityResult? I started with the camera and it works. I then added the select from gallery button with intent but the first "if"-clause is always triggered becauce REQUEST_TAKE_PHOTO is always true/1. In all the examples I could find they always used final static ints for these kind of tasks. Should I not use final static ints and set them to the correct state before triggering the onActivityState? How do people usually solve this?

   static final int REQUEST_TAKE_PHOTO = 1;
    static final int PICK_IMAGE_REQUEST = 1;
    ...
    cameraButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                ex.printStackTrace();
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.example.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }
            }
        });
galleryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");

                startActivityForResult(Intent.createChooser(intent, "Välj Bild"), PICK_IMAGE_REQUEST);
            }
        });
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {

            if (requestCode == REQUEST_TAKE_PHOTO) {
                try {
                    setPic();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else if (requestCode == PICK_IMAGE_REQUEST && data.getData() != null) {
                Uri uri = data.getData();
                mImageView.setImageURI(uri);

            }
        } else {
            Log.d("Error", "Error");
        }
    }

Solution

  • Why are using same request code fro both request . Change these.

     static final int REQUEST_TAKE_PHOTO = 1;
    static final int PICK_IMAGE_REQUEST = 1;
    

    request code should be unique to one another so use like .

     static final int REQUEST_TAKE_PHOTO = 1;
    static final int PICK_IMAGE_REQUEST = 2;
    

    onActivityresult() canbe like below .

     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_TAKE_PHOTO) {
            if (resultCode == RESULT_OK) {
                // camera picture
            } else {
                // request canceled
            }
        } else if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {
            if (data != null) {
                Uri uri = data.getData();
                mImageView.setImageURI(uri);
            }
        }
    }