我有一个应用程序,允许用户上传汽车图像和汽车的一些细节。现在我想确保每个汽车模型只能输入一个条目,所以我想运行一个方法来检查CarModel是否存在,以及是否阻止用户上传。 我该怎么做?
这是我上传图片和汽车详细信息的代码:
// Creating UploadImageFileToFirebaseStorage method to upload image on storage.
public void UploadImageFileToFirebaseStorage() {
// Checking whether FilePathUri Is empty or not.
if (FilePathUri != null) {
// Setting progressDialog Title.
progressDialog.setTitle("Image is Uploading...");
// Showing progressDialog.
progressDialog.show();
// Creating second StorageReference.
StorageReference storageReference2nd = storageReference.child(Storage_Path + System.currentTimeMillis() + "." + GetFileExtension(FilePathUri));
// Adding addOnSuccessListener to second StorageReference.
storageReference2nd.putFile(FilePathUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Getting image name from EditText and store into string variable.
String CarMake = carMake.getText().toString().trim();
String CarModel = carModel.getText().toString().trim();
// Hiding the progressDialog after done uploading.
progressDialog.dismiss();
// Showing toast message after done uploading.
Toast.makeText(getApplicationContext(), "Image Uploaded Successfully ", Toast.LENGTH_LONG).show();
@SuppressWarnings("VisibleForTests")
ImageUploadInfo imageUploadInfo = new ImageUploadInfo(taskSnapshot.getDownloadUrl().toString(), CarMake, CarModel);
// Getting image upload ID.
String ImageUploadId = databaseReference.push().getKey();
// Adding image upload id s child element into databaseReference.
databaseReference.child(ImageUploadId).setValue(imageUploadInfo);
}
})
// If something goes wrong .
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Hiding the progressDialog.
progressDialog.dismiss();
// Showing exception erro message.
Toast.makeText(uploadActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
}
})
// On progress change upload time.
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
// Setting progressDialog Title.
progressDialog.setTitle("Image is Uploading...");
}
});
}
else {
Toast.makeText(uploadActivity.this, "Please Select Image or Enter Car Details", Toast.LENGTH_LONG).show();
}
}
我还有另一种检查重复项的方法:
public void duplicateCheck(){
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
//If email exists then toast shows else store the data on new key
if (!data.getValue(Car.class).getCarModel().equals(carModel)) {
databaseReference.child(databaseReference.push().getKey()).setValue(new Car(carMake, carModel));
} else {
Toast.makeText(uploadActivity.this, "Car Model Already Exists.", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onCancelled(final DatabaseError databaseError) {
}
});
}
我在上传按钮上调用这两种方法:
// Adding click listener to Upload image button.
UploadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Calling method to upload selected image on Firebase storage.
UploadImageFileToFirebaseStorage();
duplicateCheck();
}
});