作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的代码,我可以在 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();
}
}
}
});
}
}
最佳答案
这个过程叫做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 ();
}
});
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/
我是一名优秀的程序员,十分优秀!