0

I have an android app where user can upload multiple pdf files in a "folder". My app is working perfectly for uploading and viewing a single PDF file.To upload multiple files, I know I will have to use a for loop but I don't have any idea how to proceed.

My code for uploading a single PDF is :

StorageReference pdfRef = storageReference.child(fName + "/" + pdfName);
pdfRef.putFile(pdfData)
    .addOnSuccessListener(new OnSuccessListener < UploadTask.TaskSnapshot > () {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            Toast.makeText(MainActivity.this, "File Uploaded!", Toast.LENGTH_SHORT).show();

            Task < Uri > uriTask = taskSnapshot.getStorage().getDownloadUrl();
            while (!uriTask.isComplete());
            Uri uri = uriTask.getResult();


            Map < String, Object > map = new HashMap < > ();
            map.put("url", String.valueOf(uri));
            map.put("name", pdfName);
            DocumentReference documentReference = db.collection(fName).document(pdfName);
            documentReference.set(map)
                .addOnSuccessListener(new OnSuccessListener < Void > () {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.d("TEST", "onSuccess: Task was  successful");

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.d("TEST", "onFailure: Task was not successful" + e.getLocalizedMessage() + "  " + e.getMessage());
                    }
                });
            alertDialog.dismiss();
        }
    })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

fName and pdfName are the names of the folder and PDF to be uploaded which I'm getting from somewhere else in my code. The main issue is using the for loop

Ajordat
  • 1,320
  • 2
  • 7
  • 19
Chirag
  • 98
  • 8
  • What's the question? – Alex Mamo Jul 21 '20 at 13:21
  • I want to know how to upload multiple pdf files. I have the code for uploading a single pdf file. – Chirag Jul 21 '20 at 13:36
  • Basically, I don't know how to get the uri of all selected files – Chirag Jul 21 '20 at 13:50
  • @AlexMamo can you help ? – Chirag Jul 21 '20 at 16:30
  • I think this **[answer](https://stackoverflow.com/questions/53299915/how-to-get-offline-uploaded-file-download-url-in-firebase/53300660#53300660)** might help. – Alex Mamo Jul 22 '20 at 07:14
  • @AlexMamo Hey I believe that the answer you shared works in this case. Would you mind writing it as an answer or reference your own answer on the other post in this one? – Ajordat Jul 22 '20 at 11:05
  • What do you mean through "reference your own answer on the other post in this one"? – Alex Mamo Jul 22 '20 at 11:18
  • @AlexMamo I meant that the answer on the other post was also provided by you. I was asking if you could answer this post with a reference to the shared answer. – Ajordat Jul 22 '20 at 11:20
  • @Chirag I don't understand. Did that answer help you solve the problem? If yes, I rather mark it as a duplicate than answering again. – Alex Mamo Jul 22 '20 at 11:25
  • @AlexMamo Notice that I'm not OP. Let's wait for a confirmation by him, if the answer you provided solved the issue we'll mark this as duplicate. – Ajordat Jul 22 '20 at 12:39
  • @AlexMamo Here, the single PDF I'm uploading has Uri `pdfData` . Since I'm only using `putFile` for adding a single file, how would using list() with pdfRef give me all the Uri's? The problem is selecting multiple files locally and then getting the Uri and then uploading it to firebase storage – Chirag Jul 22 '20 at 13:24
  • @Chirag You can only get the URL after it has been uploaded, not afterwards. As the [shared answer](https://stackoverflow.com/a/53300660/10810527) explains, you can use the [`list`](https://firebase.google.com/docs/reference/android/com/google/firebase/storage/StorageReference.html#public-tasklistresult-list-int-maxresults,-string-pagetoken) method on your `pdfVariable` to iterate on all the uploaded documents. – Ajordat Jul 24 '20 at 14:16

0 Answers0