[英]Validate buttons after taking a picture in Android
在 Android 中拍照后的按鈕驗證。 在我的活動中,我實現了兩個帶有默認圖像的 imageview(imageview1 和 imageview2)和打開設備相機的兩個按鈕(button1 和 button2),當從按鈕拍攝照片時,會進行驗證以更改圖像視圖圖像(button1 -> imageview1, button2 -> imageview2)。
我想做第三次驗證,它驗證照片是否已經從兩個按鈕拍攝。
我如何驗證何時已經在兩個按鈕上拍攝了照片?
這是我的代碼
ImageView imageV, imageV2;
Button btn1, btn2;
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);
btn1= findViewById(R.id.button_1);
btn2= findViewById(R.id.button_2);
btn1.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);
}
}
});
btn2.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) {
imageV.setImageResource(R.drawable.image_view_2do);
}
else if (requestCode == IMAGE_REQUEST_2) {
if (resultCode == Activity.RESULT_OK) {
imageV2.setImageResource(R.drawable.image_view2_2do);
}
}
// This is where the code fails me
//how can I validate if the photos are already taken on the two buttons?
else if (requestCode == IMAGE_REQUEST && requestCode == IMAGE_REQUEST_2) {
if (resultCode == Activity.RESULT_OK) {
imageV.setImageResource(R.drawable.image_view_2do);
imageV2.setImageResource(R.drawable.image_view2_2do);
}
}
}
}
您可以保留 2 個布爾值來跟蹤圖像是否已加載到每個 imageView 中並在onActivityResult
更新它們。
在下面的代碼中檢查isImage1Loaded
和isImage2Loaded
。
ImageView imageV, imageV2;
Button btn1, btn2;
static final int IMAGE_REQUEST = 1;
static final int IMAGE_REQUEST_2 = 2;
boolean isImage1Loaded = false, isImage2Loaded = false;
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);
btn1= findViewById(R.id.button_1);
btn2= findViewById(R.id.button_2);
btn1.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);
}
}
});
btn2.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) {
isImage1Loaded = true;
imageV.setImageResource(R.drawable.image_view_2do);
}
else if (requestCode == IMAGE_REQUEST_2) {
if (resultCode == Activity.RESULT_OK) {
image2Loaded = true;
imageV2.setImageResource(R.drawable.image_view2_2do);
}
}
// This is where the code fails me
//how can I validate if the photos are already taken on the two buttons?
else if (requestCode == IMAGE_REQUEST && requestCode == IMAGE_REQUEST_2) {
if (resultCode == Activity.RESULT_OK) {
imageV.setImageResource(R.drawable.image_view_2do);
imageV2.setImageResource(R.drawable.image_view2_2do);
}
}
}
}
我注意到您將 requestCode 的值與IMAGE_REQUEST
和 next 與IMAGE_REQUEST_2
。
在代碼中斷的部分之后,您將在一行中比較兩者。 但我相信這永遠不會奏效,因為 requestCode 只有一個值,並且您將它與兩個不同的值進行比較 [IMAGE_REQUEST, IMAGE_REQUEST_2]
我建議您每次使用該按鈕拍照時都將 IMAGE_REQUEST_2 和 IMAGE_REQUEST 添加到數組中。
然后比較該數組中是否存在兩個值,如果存在,則執行所需的操作。
像那樣:
ImageView imageV, imageV2;
Button btn1, btn2;
static final int IMAGE_REQUEST = 1;
static final int IMAGE_REQUEST_2 = 2;
private static final int PERMISSION_REQUEST = 2;
// creates a new list to store the photos taken
List<int> requestsMade = new ArrayList<int>();
@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);
btn1= findViewById(R.id.button_1);
btn2= findViewById(R.id.button_2);
btn1.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);
}
}
});
btn2.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) {
imageV.setImageResource(R.drawable.image_view_2do);
// first photo taken
requestsMade.add(IMAGE_REQUEST);
}
else if (requestCode == IMAGE_REQUEST_2) {
if (resultCode == Activity.RESULT_OK) {
imageV2.setImageResource(R.drawable.image_view2_2do);
requestsMade.add(IMAGE_REQUEST_2);
// second photo taken
}
}
// now this compares if both are taken
else if (requestsMade.contains(IMAGE_REQUEST) && requestsMade.contains(IMAGE_REQUEST_2)) {
if (resultCode == Activity.RESULT_OK) {
imageV.setImageResource(R.drawable.image_view_2do);
imageV2.setImageResource(R.drawable.image_view2_2do);
}
}
}
}
維護 2 個布爾變量
boolean isPictureTakenBtn1, isPictureTakenBtn2;
然后在 onActivityResult 中相應地更新變量
onActivityResult(int requestCode, int resultCode, Intent data) {
Uri selectedImage = null;
boolean isPictureTakenBtn1, isPictureTakenBtn2;
if (requestCode == IMAGE_REQUEST) {
if (resultCode == RESULT_OK) {
isPictureTakenBtn1 = true;
} else if (resultCode == RESULT_CANCELED) {
isPictureTakenBtn1 = false;
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
} else {
isPictureTakenBtn1 = false;
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
}
} else if (requestCode == IMAGE_REQUEST_2) {
if (resultCode == RESULT_OK) {
isPictureTakenBtn2 = true;
} else if (resultCode == RESULT_CANCELED) {
isPictureTakenBtn2 = false;
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
} else {
isPictureTakenBtn2 = false;
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
}
}
if (isPictureTakenBtn1) {
//picture capture from btn1 available, do something
/* if (data != null) {
selectedImage = data.getData();
InputStream in;
try {
in = getContentResolver().openInputStream(uri);
Bitmap selected_img = BitmapFactory.decodeStream(in);
imageV.setImageBitmap(selected_img);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "An error occured!", Toast.LENGTH_LONG).show();
}
}*/
}
if (isPictureTakenBtn2) {
//picture capture from btn2 available, do something
/* if (data != null) {
selectedImage = data.getData();
InputStream in;
try {
in = getContentResolver().openInputStream(uri);
Bitmap selected_img = BitmapFactory.decodeStream(in);
imageV2.setImageBitmap(selected_img);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "An error occured!", Toast.LENGTH_LONG).show();
}
}*/
}
}
現在您可以使用這些布爾值來檢查圖像是否已成功捕獲並根據需要執行剩余的代碼邏輯。
您可以通過布爾值來管理它,如下例所示;
ImageView imageV, imageV2; Button btn1, btn2; static final int IMAGE_REQUEST = 1; static final int IMAGE_REQUEST_2 = 2; private static final int PERMISSION_REQUEST = 2; boolean isFirstImageSet=false; boolean isSecondImageSet=false; @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); btn1= findViewById(R.id.button_1); btn2= findViewById(R.id.button_2); btn1.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); } } }); btn2.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); } } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(isFirstImageSet && isSecondImageSet){ //Both image are set } else if(isFirstImageSet ){ //First image are set } else if(isSecondImageSet){ //First image are set } }); } @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) { imageV.setImageResource(R.drawable.image_view_2do); isFirstImageSet=true; } else if (requestCode == IMAGE_REQUEST_2) { if (resultCode == Activity.RESULT_OK) { imageV2.setImageResource(R.drawable.image_view2_2do); isSecondImageSet=true; } } // This is where the code fails me //how can I validate if the photos are already taken on the two buttons? else if (requestCode == IMAGE_REQUEST && requestCode == IMAGE_REQUEST_2) { if (resultCode == Activity.RESULT_OK) { imageV.setImageResource(R.drawable.image_view_2do); imageV2.setImageResource(R.drawable.image_view2_2do); } } } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.