yugidroid
yugidroid

Reputation: 6690

onActivityResult not working with RESULT_OK, RESULT_CANCEL, etc when using camera on Android

I've got in my class something like this:

  public class Main extends Activity {

        private static final int CAMERA_PICK = 1;
        private static final int GALLERY_PICK = 2;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Button photo = (Button) findViewById(R.id.button);

            photo.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    createDialog();
                }
            });

        private void createdialog(Activity activity) {
            final CharSequence[] items = { "Take shot", "Take from gallery" };

            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setTitle("Get image");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                    if (item == 0) {
                        takePhoto();
                    }
                    if (item == 1) {
                        choosePhoto();
                    }
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }

        protected void choosePhoto() {
            // not necessary;
        }

        protected void takePhoto() {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

            mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "myPic"
                    + String.valueOf(System.currentTimeMillis()) + ".jpg"));
            cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);

            try {
                cameraIntent.putExtra("return-data", true);
                startActivityForResult(cameraIntent, CAMERA_PICK);

                //Doing something with the picture here;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // TODO
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {

             switch (resultCode) {
             case CAMERA_PICK:
                    break;
             case RESULT_OK:
                    Toast.makeText(Main.this, "Photo selected", Toast.LENGTH_SHORT).show();
break;
        }
       }
    }

As you can see I am trying to use the phone's camera to take a picture and use it later on an image view. The thing is there I can't trigger the method onActivityResult(...)! When I do the startActivityForResult(cameraIntent, CAMERA_PICK); I do not have a way to manipulate the RESULT_OK, RESULT_CANCEL or even the one I defined as CAMERA_PICK. The onActivityResult(...) should work on perfection by I am not understand what I'm doing wrong!

Any help would be aprecciated, Thanks.

I've already found the problem. I was using an activity group and I didn't realized that the onActivityResult() triggered was the first one of all the activities...

Upvotes: 1

Views: 3307

Answers (1)

Hanenoshino
Hanenoshino

Reputation: 19

I've just have the same problem when picking photo from gallery on some of the real device. After a lot of searching I found this. Indicating that the onActivityResult of the Dialog may be tirggered instead of the activity to get returned value. This issue does not happens on all devices.

Upvotes: 1

Related Questions