I have successfully uploaded multiple selected audio files to firebase storage and i am able to retrieve URLs for all of them. Since i have to upload multiple files, i used for loop for doing that. After uploading files and retreiving the URLs, i want to save them to firestore and here's what i did for doing that.

Function to upload files

private void uploadAudioFiles(List<Uri> list, final Map<String, String> hashMap) {
        if (list.size() > 0){
            for (int a=0; a<list.size(); a++){
                Uri perFile = list.get(a);
                String filename = getSelectedFileName(perFile);
                final StorageReference audioFileReference = referenceFolder.child("audioClipData/"+filename);
                final int finalA = a;
                audioFileReference.putFile(perFile).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        audioFileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(Uri uri) {
                                hashMap.put("audioFile"+ finalA, String.valueOf(uri));
                                Log.e("APP_LOG:HashMap(AudioA)", String.valueOf(hashMap));
                            }
                        });
                    }
                });
            }
        }
    }

On save button click

Below is the code which calls uploadAudioFiles() function.

Map<String, String> hashMap = new HashMap<>();
    String username = name.getText().toString();
            if (!username.isEmpty()){
                progressDialog.show();
                hashMap.put("username", username);
                uploadAudioFiles(audioClipList, hashMap);


                Log.e("APP_LOG:HashMapFINAL", hashMap.toString());
                firestore.collection(currentUser.getUid()).add(hashMap).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                    @Override
                    public void onSuccess(DocumentReference documentReference) {
                        progressDialog.dismiss();
                    }
                });
            }else {
                name.setError("Required");
            }

Here's the log which i get when i am in uploadAudioFiles() function

E/APP_LOG:HashMap(AudioA): {username=Abhi, audioFile0=https://firebasestorage.googleapis.com/v0/b/myapp.appspot.com/o/linktofile}

Here's the log which i get when i am outside of uploadAudioFiles() (ButtonClick) function

E/APP_LOG:HashMapFINAL: {username=Abhi}

Now i can't understand why the HashMap is different when i'm in uploadfile function and when i'm outside of the uploadfile function. Is this because of scope of for loop or something else?

can someone help me to understand the situation and solution of the situation?

Related posts

Recent Viewed