Home > Net >  Firestore saves image url and user data only once , how can i resolve this?
Firestore saves image url and user data only once , how can i resolve this?

Time:11-15

I have 2 methods, the first one stores for cloud storage the other method(Savingtofirestore) saves Item data to firestore and downloads image url .I have a collection called Images and within it i have a document that has an item of the authenticated user .


private void uploadImageToStorage() {
    if (filepath != null) {

            // Code for showing progressDialog while uploading
            ProgressDialog progressDialog
                    = new ProgressDialog(AddItem.this);
            progressDialog.setTitle("Uploading...");
            progressDialog.show();

            // Defining the child of storageReference
            StorageReference ref
                    = storageReference
                    .child(
                            "images/"
                                      UUID.randomUUID().toString());
            // adding listeners on upload
            // or failure of image
            ref.putFile(filepath)
                    .addOnSuccessListener(
                            new OnSuccessListener<UploadTask.TaskSnapshot>() {

                                @Override
                                public void onSuccess(
                                        UploadTask.TaskSnapshot taskSnapshot)
                                {
                                    Task<Uri> uriTask = taskSnapshot.getStorage().getDownloadUrl();
                                    while(!uriTask.isSuccessful());

                                    downloadurl = uriTask.getResult().toString();
                                    if(uriTask.isSuccessful())
                                    {

                                        SavingToFirestore(downloadurl);
                                    }
                                    // Image uploaded successfully
                                    // Dismiss dialog
                                    progressDialog.dismiss();
                                    /*Toast
                                            .makeText(AddItem.this,
                                                    "Image Uploaded!!",
                                                    Toast.LENGTH_SHORT)
                                            .show();

                                     */
                                }
                            })

                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e)
                        {

                            // Error, Image not uploaded
                            progressDialog.dismiss();
                            Toast
                                    .makeText(AddItem.this,
                                            "Failed "   e.getMessage(),
                                            Toast.LENGTH_SHORT)
                                    .show();
                        }
                    })
                    .addOnProgressListener(
                            new OnProgressListener<UploadTask.TaskSnapshot>() {

                                // Progress Listener for loading
                                // percentage on the dialog box
                                @Override
                                public void onProgress(
                                        UploadTask.TaskSnapshot taskSnapshot)
                                {
                                    double progress
                                            = (100.0
                                            * taskSnapshot.getBytesTransferred()
                                            / taskSnapshot.getTotalByteCount());
                                    progressDialog.setMessage(
                                            "Uploaded "
                                                      (int)progress   "%");
                                }
                            });
        }
    }

private void SavingToFirestore(String url){

// String id = f.getCurrentUser().getUid();

firebaseFirestore.collection("users").document(user_id).get()
        .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                Map<String,String> userMap = new HashMap<>();

                userMap.put("name",name);
                userMap.put("price",price);
                userMap.put("description",description);
                userMap.put("image",downloadurl.toString());
                firebaseFirestore.collection("images").document(user_id).set(userMap)
                        .addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
             public void onSuccess(Void aVoid) { //if the data is saved successfully return to the main activity
                                itemName.setText("");
                                itemPrice.setText("");
                                itemDescription.setText("");
                                itemImage.setImageResource(R.drawable.itemphoto);
                                Toast.makeText(AddItem.this,"Item Upload successfully           
  • Related