获取用于在 Android 上调用相机意图的原始按钮的标识符
如果我在视图上有多个按钮来调用相机意图(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)和一个用于预览每个图像的 ImageView,并且我需要知道哪个按钮在 onActivityResult 中调用它,以便我知道要使用哪个相应的预览我要传递一个识别变量吗?以下是仅适用于一张图像的当前代码。
图片按钮:
final ImageButton cameraTakePhotoButton = (ImageButton) photoPromptOption.findViewById(R.id.cameraTakePhotoButton);
cameraTakePhotoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(requestCode == CAMERA_PIC_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
final ImageView questionPhotoResult = (ImageView) findViewById(R.id.questionPhotoResult);
questionPhotoResult.setImageBitmap(thumbnail);
}
}
}
If I have multiple buttons on a view to call camera intent (android.provider.MediaStore.ACTION_IMAGE_CAPTURE) and a ImageView for the preview of each image and I need to know which button called it in onActivityResult so I know which corresponding preview to use how do I pass an identifying variable? Below is current code that only works with one image.
Picture button:
final ImageButton cameraTakePhotoButton = (ImageButton) photoPromptOption.findViewById(R.id.cameraTakePhotoButton);
cameraTakePhotoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(requestCode == CAMERA_PIC_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
final ImageView questionPhotoResult = (ImageView) findViewById(R.id.questionPhotoResult);
questionPhotoResult.setImageBitmap(thumbnail);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最终使用 ListView 和自定义适配器,然后让它使用 Photo 类的对象迭代 ArrayList...当我拍照时,我会更新 Photo 对象的位图(缩略图)属性,然后刷新 ListView。这个方法效果很好。
然后我的 onActivityResult:
Ended up using a ListView and custom adapter then having it iterate over an ArrayList with objects of class Photo... I update the Bitmap (thumbnail) property for the Photo objects when I take the picture then refresh the ListView. This method works very well.
Then my onActivityResult:
哎呀。为什么不使用 requestCode 呢?只需用唯一的代码标记每个视图,确保它不会与您抛出的任何其他意图发生冲突。或者,您可以使用 对象。 hashCode() ,但再次注意冲突。
然后在处理程序中检查它:
如果您有许多要以类似方式处理的按钮(或 ListView 中的项目),则可以使用 Map 从标记恢复调用视图。
Yikes. Why not use requestCode? Just tag each view with a unique code, making sure it doesn't clash with any other intents you're throwing around. Alternatively, you can use Object.hashCode() , but again, watch out for clashes.
Then check it in your handler:
If you have many buttons (or items in a ListView) that you're going to handle similarly, you can use a Map to recover the calling view from the tag.