Search code examples
androidandroid-activityandroid-intentextras

Can't get intent extras in android app


I'm trying to put extras (string) in an intent. I use startActivityForResult and onActivityResult to get my extras on the other side.

But I can't get why it doesn't works ! Here's my code :

    buttonCamera.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra("abc", "test");
            startActivityForResult(intent, PHOTO_RESULT);

        }
    }); 

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PHOTO_RESULT) {
        if (resultCode == RESULT_OK) {

            Bundle extras = data.getExtras();
            if (extras != null) {
                String abc = extras.getString("abc");
                Toast.makeText(getApplicationContext(), abc, Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "can't get", Toast.LENGTH_SHORT).show();
            }

        }
    }
}

I always get an empty toast, so the extra is not null.. But I cant't get the String..

Thanks !


Solution

  • edit: To tell the camera the file name you can use this:

    File mFile = new File(path, filename);
    final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));
    activity.startActivityForResult(intent, TAKE_PHOTO_CODE);
    

    The camera activity will get your extra intent data, but it won't return that "abc" string. You can't tell the camera activity to return "abc" for results. What are you trying to do with that abc string?