开发者

Can't get intent extras in android app

开发者 https://www.devze.com 2023-03-22 18:47 出处:网络
I\'m trying to put extras (string) in an intent. I use startActivityForResult and onActivityResult to get my extras on the other side.

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 ext开发者_开发技巧ras = 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 !


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?


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 .

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号