Android 大家好,我对Fireabse Firestore和Firebase存储有严重问题。安卓工作室
问题是,当用户上传自己的照片时,我可以使用当前UID将照片上传到firebase存储中。但我无法将其检索到firebase firestore数据库(无法将配置文件照片URL写入配置文件字符串) 我在firebase存储中获得了UID。但我无法将其自动写入firestore数据库中的配置文件字符串。 以下是我的个人资料片段中的完整代码。如果有人能为我重新编写这些代码,这将非常有帮助。因为我手动将每个个人资料URL放置到每个用户的个人资料字符串中 这是我的密码 公共类ProfileFragment扩展了该片段{Android 大家好,我对Fireabse Firestore和Firebase存储有严重问题。安卓工作室,android,firebase,android-studio,google-cloud-firestore,Android,Firebase,Android Studio,Google Cloud Firestore,问题是,当用户上传自己的照片时,我可以使用当前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
}
private-Uri-imageUri;
私有位图压缩器;//现在无法使用它
私有进程对话;
私有存储参考存储参考;
私有FirebaseAuth FirebaseAuth;
私人FirebaseFirestore FirebaseFirestore;
公共档案片段(){
//必需的空公共构造函数
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
CircleImageView图像视图;
碎片绑定;
FirebaseFirestore数据库;
火基储存;
FirebaseAuth-auth;
EditText name;//名称文本框
User;//用户类
按钮更新;//更新按钮
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
绑定=碎片绑定。充气(充气机、容器、假);
database=FirebaseFirestore.getInstance();
auth=FirebaseAuth.getInstance();
storage=FirebaseStorage.getInstance();
progressDialog=新建progressDialog(getContext());
firebaseAuth=firebaseAuth.getInstance();
StorageReference=FirebaseStorage.getInstance().getReference().child(“ProfilePictures”);
//从firebase获取当前用户的完整详细信息
imageView=binding.profileImage;
update=binding.updateBtn;
imageView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意图=新意图();
intent.setAction(intent.ACTION\u GET\u CONTENT);
intent.setType(“image/*”);
//无检查弃用
startActivityForResult(意向,3);
}
});
数据库集合(“用户”)
.document(FirebaseAuth.getInstance().getUid())//从数据库获取唯一id
.get().addOnSuccessListener(新的OnSuccessListener()){
@凌驾
成功时公共无效(文档快照文档快照){
user=documentSnapshot.toObject(user.class);//将用户对象转换为类对象
binding.nameBox.setText(String.valueOf(user.getName());
binding.emailBox.setText(String.valueOf(user.getEmail());
//将(user.getProfile())加载到(binding.profileImage)中;
}
});
返回binding.getRoot();
}
@抑制警告(“弃用”)
@凌驾
activityresult上的公共void(int-requestCode、int-resultCode、@Nullable-Intent-data){
super.onActivityResult(请求代码、结果代码、数据);
if(data.getData()!=null){
Uri profileUri=data.getData();
setImageURI(profileUri);
update.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
updateUserProfile();
progressDialog.setMessage(“上传你可爱的照片…”);
progressDialog.show();
File newFile=新文件(profileUri.getPath());
最终StorageReference=storage.getReference().child(“ProfilePictures”)
.child(FirebaseAuth.getInstance().getUid());
reference.putFile(profileUri).addOnSuccessListener(新的OnSuccessListener()){
@凌驾
成功时公共无效(UploadTask.TaskSnapshot TaskSnapshot){
progressDialog.disclose();
Toast.makeText(getContext(),“照片上传!”。在检查照片时进行小测验”,Toast.LENGTH\u LONG.show();
}
});
}//这是我个人资料片段中的完整代码
//您可以修复或删除任何代码以获得最佳输出
//这些是我的firebase firestore数据库//
//用户>文档id>“名称”“配置文件”“电子邮件”“通行证”
});//配置文件用于配置文件图片
//database=“ProfilePictures”
}
}
私有void updateUserProfile(){
Uri下载\u Uri;//我在这里有点困惑。
下载_uri=imageUri;
Map userdata=newhashmap();
userdata.put(“profile”,下载_uri.toString());
firebaseFirestore.collection(“Users”).document(firebaseAuth.getUid()).set(userdata).addOnCompleteListener(新OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
progressDialog.disclose();
Toast.makeText(getContext(),“successful”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(getContext(),“Firestore错误:+task.getException().getMessage(),Toast.LENGTH_SHORT).show();
}
}
});//每当我运行应用程序时,如果我单击
“上载”按钮应用程序正在关闭。也不是
将其存储到存储,而不是检索到
firestore数据库
}
我已经解决了这个问题。但我还有一个错误。当我将图像URI上载到firebase firestore时
Map<String,String > profile = new HashMap<>();
profile.put("profile",imageUri.toString());
database
.collection("Users") // the path of users.
.document(FirebaseAuth.getInstance().getUid()) // to update in the current users.
// .update(user)
.set(profile)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Toast.makeText(getContext(), "Photo Updated!", Toast.LENGTH_SHORT).show();
}
});
}
database.collection("Users") // the path of users.
.document(FirebaseAuth.getInstance().getUid())
.update("profile",imageUri.toString())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "Profile Image path successfully updated!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error updating Profile Image path", e);
}
});