【问题标题】:Hello Everyone I have a serious problem with Fireabse Firestore and the Firebase storage. Android Studio大家好,我对 Firebase Firestore 和 Firebase 存储有一个严重的问题。安卓工作室
【发布时间】: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


【解决方案1】:

这与您在评论中描述的问题无关; 如果您只想修改/更新一个字段,您可以使用更新方法而不是set 方法。如果单独使用set 方法而不定义合并选项,文档将被覆盖。

所以代码应该写成;

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);
            }
        });

【讨论】:

    【解决方案2】:

    我已经解决了这个问题。但我还有另一个错误。当我将图像 URI 上传到 firebase firestore 数据库时,其他数据都消失了,只有配置文件 URI 可见。

    目前,我有 5 个字符串 姓名 电子邮件 密码 个人资料(图片 URI) 参考编号 硬币

    但是,当用户上传他们的个人资料照片时。所有数据均已删除。

    仅显示个人资料图片 URI

    我已在此处附上代码

    private void updateUserProfile() {

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

    【讨论】:

      猜你喜欢
      • 2019-04-06
      • 1970-01-01
      • 2016-03-03
      • 2018-12-09
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      • 1970-01-01
      相关资源
      最近更新 更多