Prashant Singh
Prashant Singh

Reputation: 3793

onActivityResult not getting triggered in Android

I am making an android application, which takes an image from camera and then displays it. However, I am unable to display the clicked image probably because the onActivityResult() is not triggered.

Here is my piece of code. Can anyone suggest me what am i missing ?

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final int CAMERA_PIC_REQUEST = 1337; 
        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

            }
            @override
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                Log.d("Message1", "I reached 2");
                 //super.onActivityResult(requestCode, resultCode, data);
                if (requestCode == CAMERA_PIC_REQUEST) {
                    // do something
                    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");  
                    ImageView image = (ImageView) findViewById(R.id.imageView1);  
                    image.setImageBitmap(thumbnail); 

                }
            }




        });
    }
} 

Upvotes: 1

Views: 2296

Answers (1)

hopia
hopia

Reputation: 4996

onActivityResult() must be declared in your Activity class (not inside the onClickListener). If you correct the "@override" ('o' must be capitalized), typo before your current onActivityResult() declaration, you'll see what I mean...

See the Activity.onActivityResult() documentation.

Here's how your class should look like:

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final int CAMERA_PIC_REQUEST = 1337; 
        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

            }
        });
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
       Log.d("Message1", "I reached 2");
       //super.onActivityResult(requestCode, resultCode, data);
       if (requestCode == CAMERA_PIC_REQUEST) {
         // do something
         Bitmap thumbnail = (Bitmap) data.getExtras().get("data");  
         ImageView image = (ImageView) findViewById(R.id.imageView1);  
         image.setImageBitmap(thumbnail); 
       }
     }

} 

Upvotes: 1

Related Questions