【发布时间】:2021-08-22 13:48:20
【问题描述】:
enter image description here问题是当他们上传自己的照片时,我可以使用当前 UID 将用户照片上传到我的 firebase 存储。但我无法将其检索到我的 firebase firestore 数据库(无法将个人资料照片 URL 写入个人资料字符串。
我在 Firebase 存储中获得了 UID。但我无法将其自动写入我的 firestore 数据库中的配置文件字符串。 这是我的个人资料片段中的完整代码。如果任何人都可以为我重写此代码,那将非常有帮助。因为我手动将每个配置文件 URL 放置到每个用户配置文件字符串。
这是我的代码
公共类 ProfileFragment 扩展片段 {
private Uri imageUri;
private Bitmap compressor; // cant use it now
private ProgressDialog progressDialog;
private StorageReference StorageReference;
private FirebaseAuth firebaseAuth;
private FirebaseFirestore firebaseFirestore;
public ProfileFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
CircleImageView imageView;
FragmentProfileBinding binding;
FirebaseFirestore database;
FirebaseStorage storage;
FirebaseAuth auth;
EditText name; // name text box
User user; // user class
Button update; //update button
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
binding = FragmentProfileBinding.inflate(inflater, container, false);
database = FirebaseFirestore.getInstance();
auth = FirebaseAuth.getInstance();
storage = FirebaseStorage.getInstance();
progressDialog = new ProgressDialog(getContext());
firebaseAuth = FirebaseAuth.getInstance();
StorageReference = FirebaseStorage.getInstance().getReference().child("ProfilePictures");
// to get current users full details from firebase
imageView = binding.profileImage;
update = binding.updateBtn;
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
//noinspection deprecation
startActivityForResult(intent, 3);
}
});
database.collection("Users")
.document(FirebaseAuth.getInstance().getUid()) // getting the unique id from database
.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
user = documentSnapshot.toObject(User.class);//converting user object to class object
binding.nameBox.setText(String.valueOf(user.getName()));
binding.emailBox.setText(String.valueOf(user.getEmail()));
// Glide.with(imageView) .load(user.getProfile()) .into(binding.profileImage);
}
});
return binding.getRoot();
}
@SuppressWarnings("deprecation")
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data.getData() != null) {
Uri profileUri = data.getData();
imageView.setImageURI(profileUri);
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateUserProfile();
progressDialog.setMessage("Uploading your cute photo..");
progressDialog.show();
File newFile = new File(profileUri.getPath());
final StorageReference reference = storage.getReference().child("ProfilePictures")
.child(FirebaseAuth.getInstance().getUid());
reference.putFile(profileUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(getContext(), "Photo Uploaded!. Play Quiz while We checking your photo", Toast.LENGTH_LONG).show();
}
});
} // so this is my full code in my profile fragment
// you can fix or remove any code for the best output
// These are my firebase firestore database //
// User > document id > "name" "profile" "email" "pass"
}); // profile is for profile picture
// database = "ProfilePictures"
}
}
private void updateUserProfile() {
Uri download_uri; // iam little bit confused here.
download_uri = imageUri;
Map<String, String > userdata = new HashMap<>();
userdata.put("profile",download_uri.toString());
firebaseFirestore.collection("Users").document(firebaseAuth.getUid()).set(userdata).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
progressDialog.dismiss();
Toast.makeText(getContext(), "Succesful", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getContext(), "Firestore Error:" +task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
}); //whenever I run the app, if I click the
upload button the app is closing. also not
storing it to storage, and not retrieve to
firestore database
}
【问题讨论】:
-
这段代码到底有什么问题?
-
你好亚历克斯马莫。问题是我无法将用户图像上传到 firebase 存储,也无法将其检索到我的 firebase firestore 数据库。如果你能帮助我,我可以与你分享我的 Fragment 和布局 XML..
标签: android firebase android-studio google-cloud-firestore