无法在 Android 应用程序中获取意图附加内容
我正在尝试将额外内容(字符串)放入意图中。我使用 startActivityForResult 和 onActivityResult 在另一侧获取额外内容。
但我不明白为什么它不起作用!这是我的代码:
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'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 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:
要告诉相机文件名,您可以使用以下命令:
相机活动将获取额外的意图数据,但不会返回“abc”字符串。您无法告诉相机活动返回“abc”以获得结果。你想用这个 abc 字符串做什么?
edit:
To tell the camera the file name you can use this:
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?
显然你会得到一个空的吐司。
您为意图 new Intent(MediaStore.ACTION_IMAGE_CAPTURE) 添加了额外的内容,并且您在 onActivity 结果中获取的意图数据不同(从相机活动返回)。
所以将值存储在类变量中就可以正常工作。
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 .