gpt4 book ai didi

java - 如何使用Java上传上传到Fire Storage的图像Url并将其同时保存在fireStore中

转载 作者:行者123 更新时间:2023-12-04 10:00:58 24 4
gpt4 key购买 nike

这是我的代码,我可以在 Toast 消息中检索图像 Url,但无法将其保存在 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();

}
}




}
});
}


}

并且在 fire store 中,该字段总是为 null 但它保存在 firestorage 中。

想为 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 ();
}
});

这里确保先上传图像,然后将 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();

}
});

}
});
}
});
}

}

关于java - 如何使用Java上传上传到Fire Storage的图像Url并将其同时保存在fireStore中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61826518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com