Bhavesh Patadiya
Bhavesh Patadiya

Reputation: 25830

Not getting able to call onActivityResult Method

hi i am not able to getting call to method onActivityResult after below code.

private void ImageChooseOptionDialog() {

    Log.i(TAG, "Inside ImageChooseOptionDialog");
    String[] photooptionarray = new String[] { "Take a photo",
            "Choose Existing Photo" };

    Builder alertDialog = new AlertDialog.Builder(TabSample.tabcontext)
            .setItems(photooptionarray,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            if (which == 0) {
                                Log.i(TAG, "Which" + which);
                                Log.i(TAG,
                                        "Inside ImageChooseOptionDialog Camera");
                                _path = Environment
                                        .getExternalStorageDirectory()
                                        + File.separator
                                        + "TakenFromCamera.png";

                                Log.d(TAG, "----- path ----- " + _path);
                                media = _path;
                                // File file = new File(_path);
                                // Uri outputFileUri = Uri.fromFile(file);
                                // Intent intent = new Intent(
                                // android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                                // intent.putExtra(MediaStore.EXTRA_OUTPUT,
                                // outputFileUri);
                                // startActivityForResult(intent, 1212);

                            } else if (which == 1) {
                                Log.i(TAG, "Which" + which);
                                Log.i(TAG,
                                        "Inside ImageChooseOptionDialog Gallary");
                                // Intent intent = new Intent();
                                // intent.setType("image/*");
                                // intent.setAction(Intent.ACTION_GET_CONTENT);
                                // startActivityForResult(Intent
                                // .createChooser(intent,
                                // "Select Picture"), 1);

                                Intent intent = new Intent(
                                        Intent.ACTION_GET_CONTENT);
                                intent.setType("image/*");
                                // intent.setAction(Intent.ACTION_GET_CONTENT);
                                startActivityForResult(Intent
                                        .createChooser(intent,
                                                "Select Picture"), 1);

                                Log.i(TAG, "end" + which);

                            }
                        }
                    });
    alertDialog.setNeutralButton("Cancel",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    alertDialog.create().show();
}

and this is my onActivityResult method:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i(TAG, "Inside Onactivity Result");
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            Log.i(TAG, "Inside if else Onactivity Result");
            // currImageURI is the global variable I’m using to hold the
            // content:// URI of the image
            Uri currImageURI = data.getData();
            String path = getRealPathFromURI(currImageURI);
            Log.i(TAG, "Inside Onactivity Result Image path" + path);

        }
    }
}

please let me knew where i am doing wrong. i am calling onActivityResult method from which=1 after dialog box appears. but not getting any log inside onActivityResult method in logcat.

Upvotes: 0

Views: 450

Answers (1)

Shrikant Ballal
Shrikant Ballal

Reputation: 7087

This is because, you may not be getting RESULT_OK. Always first check for request code and inside that check for result code. If you are retrieving image from gallery, try following code inside onActivityResult():

if (requestCode == TAKE_PICTURE_GALLERY) {
            if (resultCode == RESULT_OK) {

                final Uri selectedImage = data.getData();
                final String[] filePathColumn = { MediaStore.Images.Media.DATA };

                final Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                final int columnIndex = cursor
                        .getColumnIndex(filePathColumn[0]);
                final String filePath = cursor.getString(columnIndex);
                cursor.close();
             }
}

And use the filePath wherever you want. I hope this solves your problem. Thank you :)

UPDATE: Use this code to start your gallery activity:

 imagePathURI = Uri.fromFile(new File(<your image path>));
        final Intent intent = new Intent(Intent.ACTION_PICK, imagePathURI);
                intent.setType("image/*");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imagePathURI);
                startActivityForResult(intent, TAKE_PICTURE_GALLERY);

When you want to retrieve image from gallery, the MediaStore.EXTRA_OUTPUT refers to The name of the Intent-extra used to indicate a content resolver Uri to be used to store the requested image or video. So here, you have to pass the image path where u'll receive your image. imagePathURI = Uri.fromFile(new File(<your image path>)); // here a file will be created from your image path. And when you'll receive an image, u can access the image from this image path.

Upvotes: 1

Related Questions