0

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 !

beluga
  • 193
  • 3
  • 17

2 Answers2

0

Obviously you will get an empty toast . You are putting extra for the intent new Intent(MediaStore.ACTION_IMAGE_CAPTURE) and the Intent data you are getting in onActivity result is different , (Returned from Camera Activity ) . so store value in a class variable will work fine .

Shailendra Singh Rajawat
  • 8,172
  • 3
  • 35
  • 40
  • I don't understand in what it is a different one.. The intent we have in parameter in the onActivityResult is the intent we called earlier, no ? – beluga Jul 22 '11 at 13:23
  • Just use a class variable. In your main activity, add `public String abc` before the `onCreate()` method. Now later you can change it by `MainActivity.abc = "test";` – Rob Jul 22 '11 at 14:11
0

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?

Community
  • 1
  • 1
Vector
  • 359
  • 2
  • 6
  • I do some "tests" for the moment, but in the end it will be the path of a file where my image will be saved. That's why I try to send a string by this intent. – beluga Jul 22 '11 at 14:05
  • please use the code in my answer. You can save the file name as a class variable. – Vector Jul 22 '11 at 14:29