拍照后如何验证不同的按钮?



我有2个imageview有一个特定的图像发送到设备的相机,我想验证,我想当照片被拍摄时,它改变图像取决于从哪个按钮拍摄的照片

我试着这样做,但它不工作。

ImageView imageV, imageV2;
static final int IMAGE_REQUEST = 1;
private static final int PERMISSION_REQUEST = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageV = findViewById(R.id.image_view);
imageV2 = findViewById(R.id.image_view_2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (camara.resolveActivity(getPackageManager()) != null) {
startActivityForResult(camara, IMAGE_REQUEST);
}
}
});
btnIne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (camara.resolveActivity(getPackageManager()) != null) {
startActivityForResult(camara, IMAGE_REQUEST);
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
//Images to change
imageV.setImageResource(R.drawable.image_view_2do);
imageV2.setImageResource(R.drawable.image_view2_2do);
}
else {
//Default images
imageV.setImageResource(R.drawable.image_view_1er);
imageV2.setImageResource(getResources().getString(R.string.image_view2_1er));
}
}
}

您必须在startActivityForResult中添加不同的请求代码。就像你对两个按钮都使用了IMAGE_REQUEST一样。

示例:

ImageView imageV, imageV2;
static final int IMAGE_REQUEST = 1;
static final int IMAGE_REQUEST_2 = 2;
private static final int PERMISSION_REQUEST = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageV = findViewById(R.id.image_view);
imageV2 = findViewById(R.id.image_view_2);
btnStatementAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (camara.resolveActivity(getPackageManager()) != null) {
startActivityForResult(camara, IMAGE_REQUEST);
}
}
});
btnIne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent camara = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (camara.resolveActivity(getPackageManager()) != null) {
startActivityForResult(camara, IMAGE_REQUEST_2);
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
//Images to change
imageV.setImageResource(R.drawable.image_view_2do);
imageV2.setImageResource(R.drawable.image_view2_2do);
}
else if (requestCode == IMAGE_REQUEST_2) {
if (resultCode == Activity.RESULT_OK) {
//Images to change
imageV.setImageResource(R.drawable.image_view_2do);
imageV2.setImageResource(R.drawable.image_view2_2do);
}
else {
//Default images
imageV.setImageResource(R.drawable.image_view_1er);
imageV2.setImageResource(getResources().getString(R.string.image_view2_1er));
}
}
}

相关内容

  • 没有找到相关文章

最新更新