rootNode = FirebaseDatabase.getInstance();
reference = rootNode.getReference("User");
// Get all the values
String name = Objects.requireNonNull(regName.getEditText()).getText().toString().trim();
String email = Objects.requireNonNull(regEmail.getEditText()).getText().toString().trim();
String phoneNo = Objects.requireNonNull(regPhone.getEditText()).getText().toString().trim();
String password = Objects.requireNonNull(regPass.getEditText()).getText().toString().trim();
String address = Objects.requireNonNull(regAddress.getEditText()).getText().toString().trim();
int genderId = gender.getCheckedRadioButtonId();
String gender = (genderId == R.id.radioButtonMale) ? "Male" : "Female";
String college = autoCompleteTextCollege.getText().toString().trim();
String year = autoCompleteTextYear.getText().toString().trim();
String department = autoCompleteTextDept.getText().toString().trim();
String division = autoCompleteTextDivision.getText().toString().trim();
String batch = autoCompleteTextBatch.getText().toString().trim();
// Check if any field is empty
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(email) || TextUtils.isEmpty(phoneNo) ||
TextUtils.isEmpty(password) || TextUtils.isEmpty(address) || TextUtils.isEmpty(gender) ||
TextUtils.isEmpty(college) || TextUtils.isEmpty(year) || TextUtils.isEmpty(department) ||
TextUtils.isEmpty(division) || TextUtils.isEmpty(batch)) {
Toast.makeText(Registration.this, "All fields are required", Toast.LENGTH_SHORT).show();
return;
}
UserHelperClass helperClass = new UserHelperClass(name,email,password,phoneNo,address,gender,college,year,department,division,batch);
reference.child(phoneNo).setValue(helperClass);
String userID = phoneNo;
StorageReference profilePhotoRef = FirebaseStorage.getInstance().getReference("profile_photo/"+userID+".jpg");
if (filePath != null){
profilePhotoRef.putFile(filePath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
profilePhotoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String profilePhotoUrl = uri.toString();
helperClass.setProfilePhotoUrl(profilePhotoUrl);
if (collegeIdFilePath != null){
StorageReference collegeIdPhotoRef = FirebaseStorage.getInstance().getReference().child("collegeIdPhotos").child(userID + ".jpg");
collegeIdPhotoRef.putFile(collegeIdFilePath)
.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()){
collegeIdPhotoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String collegeIdPhotoUrl = uri.toString();
helperClass.setCollegeIdPhotoUrl(collegeIdPhotoUrl);
reference.child(userID).setValue(helperClass);
Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
}
});
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),"College ID card photo uploade fail",Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(getApplicationContext(),"College ID card Not selected",Toast.LENGTH_SHORT).show();
}
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),"Profile photo upload fail",Toast.LENGTH_SHORT).show();
}
});
}
}
});
我编写了这段代码,用于插入用户注册期间拍摄的图像。但是,它给我一个错误,提示“个人资料照片上传失败。”我使用 Java for Android Studio,使用的数据库是 Firebase 数据库。
期望代码能够在 android studio 中使用 java 处理将图像插入到 firebase 中
尝试以下方法
if (filePath != null) {
profilePhotoRef.putFile(filePath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
profilePhotoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String profilePhotoUrl = uri.toString();
helperClass.setProfilePhotoUrl(profilePhotoUrl);
if (collegeIdFilePath != null) {
// ...
} else {
Toast.makeText(getApplicationContext(), "College ID card Not selected", Toast.LENGTH_SHORT).show();
}
// here display success message and navigate to login screen
Toast.makeText(getApplicationContext(), "Profile photo uploaded successfully", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(), "Profile photo upload failed", Toast.LENGTH_SHORT).show();
}
});
}
如果出现任何问题请告诉我?