这是我的代码,我可以在Toast消息中检索图像网址,但无法将其保存在Firestore中,请告诉我是否还有另一种方法可以解决,或者是我存储URL的问题:
public class register extends AppCompatActivity {
EditText emails;
EditText passwords;
EditText ages;
Button registers;
Button addImage;
EditText country;
FirebaseFirestore db;
FirebaseAuth auth;
FirebaseUser auth1;
private StorageTask task;
Uri url ;
Uri imageUri;
static final int IMAGE_REQUEST =2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
auth = FirebaseAuth.getInstance();
registers = findViewById(R.id.button);
emails = findViewById(R.id.eamil);
passwords = findViewById(R.id.password);
ages=findViewById(R.id.age);
country=findViewById(R.id.country);
addImage = findViewById(R.id.button51);
db =FirebaseFirestore.getInstance();
registers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String v_email = emails.getText().toString();
String v_password = passwords.getText().toString();
if (TextUtils.isEmpty(v_email)||TextUtils.isEmpty(v_password)){
Toast.makeText(register.this,"Eempty Credentials",Toast.LENGTH_SHORT).show();
}else if(v_password.length()<6){
Toast.makeText(register.this,":password is short",Toast.LENGTH_SHORT).show();
}else{
upload();
registerUser(v_email, v_password);
}
}
});
addImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openImage();
}
});
}
private void registerUser(final String email, final String password) {
auth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(register.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Map<String , Object> users = new HashMap<>();
users.put("Email", emails.getText().toString());
users.put("Password", passwords.getText().toString());
users.put("Country", country.getText().toString());
users.put("AGe", ages.getText().toString());
users.put("Image", url.toString());
auth1 = FirebaseAuth.getInstance().getCurrentUser();
db.collection("Users").document(auth1.getUid()).set(users).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
//Toast.makeText(com.example.pleasework.register.this,"registeration was successful",Toast.LENGTH_SHORT).show();
}
});
// startActivity(new Intent(com.example.pleasework.register.this, MainActivity.class));
// finish();
}else{
Toast.makeText(com.example.pleasework.register.this,"Resgisteration fail",Toast.LENGTH_SHORT).show();
}
}
});
}
public String getFileExtension(Uri uri){
ContentResolver contentResolver = getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
}
public void upload (){
if (imageUri != null){
final StorageReference fileRef = FirebaseStorage.getInstance().getReference(System.currentTimeMillis()+getFileExtension(imageUri) );
fileRef.putFile(imageUri)
.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
fileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
url = uri;
Toast.makeText(register.this,"Upload Sucess" + url.toString(),Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
public void openImage(){
Intent intent = new Intent();
intent.setType("image/");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
imageUri=data.getData();
}
}
}
});
}
}
并且在火灾存储区中,字段始终为空,但将其保存在火灾存储区中。
想为FireStore添加图像,但没有证据
谢谢。
此过程称为asynctask,旨在在过程之前执行过程。
它可以同时上传图像和数据,因此图像链接没有存储,因为它还没有完成图像的上传,而且时间不到一秒钟。
当图像上传完成后,可以使用此方法,数据存储在FireStore中:
private void registerUser(final String email, final String password) {
auth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(register.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
upload ();
}
});
[Here请确保首先上传图像,然后将ImageUrl添加到fireStore:
public void upload (){
if (imageUri != null){
final StorageReference fileRef = FirebaseStorage.getInstance().getReference(System.currentTimeMillis()+getFileExtension(imageUri) );
fileRef.putFile(imageUri)
.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
fileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
url = uri;
Map<String , Object> users = new HashMap<>();
users.put("Email", emails.getText().toString());
users.put("Password", passwords.getText().toString());
users.put("Country", country.getText().toString());
users.put("AGe", ages.getText().toString());
users.put("Image", url.toString());
auth1 = FirebaseAuth.getInstance().getCurrentUser();
db.collection("Users").document(auth1.getUid()).set(users).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(register.this,"Upload Sucess" + url.toString(),Toast.LENGTH_SHORT).show();
}
});
}
});
}
});
}
}