Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Android: How to cancel download?

Tags:

How can I cancel my downloading task from Firebase?

I want to cancel the download whenever I click somewhere off the ProgressDialog.

Here is the part where my Download Activity ExamesActivity.java is. It looks like:

//Download the File on Button(Download) click:
        bDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Initalizing teh Spinner-to-String functions:
                Grade = spClasse.getSelectedItem().toString();
                Type = spEpoca.getSelectedItem().toString();
                Subject = spDisciplina.getSelectedItem().toString();
                Year = spAno.getSelectedItem().toString();

                //Download the File:
                //First Check if ON the Spinner, everything is choosen. It should be. If not, show error Toast.
                if (Grade.equals("...") | Type.equals("...") | Disciplina.equals("...") | Year.equals("..."){

                    //Show the The Error Toast:
                    Toast.makeText(ExamesActivity.this, "everything shall be choosen", Toast.LENGTH_SHORT).show();

                } else {                                          //What the dir would look like: "Subject/Grade/Year-Type.extension"
                    pdfRef = mStorageRef.child(Subject + "/" + Grade + "/" + Year + "-" + Type + ".pdf");
                    File root = android.os.Environment.getExternalStorageDirectory();
                    File dir = new File(root.getAbsolutePath() + "/Exams-App/");

                    //Show the ProgressDialog while downloading:
                    progressDialog.show();

                    if (!dir.exists()) {
                        dir.mkdirs();
                    }

                    localFile = new File(dir, Subject + "-" + Year + "-" + Grade + "-" + Type + ".pdf");


                    pdfRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                            // Local temp file has been created
                            progressDialog.dismiss();
                            Toast.makeText(ExamesActivity.this, "Exam was successfully downloaded!️",  Toast.LENGTH_SHORT).show();
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception exception) {
                            // Handle any errors
                            progressDialog.dismiss();
                            Toast.makeText(ExamesActivity.this, "Exam not found on the server.", Toast.LENGTH_LONG).show();
                        }

                    }).addOnProgressListener(new OnProgressListener<FileDownloadTask.TaskSnapshot>() {
                        @Override
                        public void onProgress(FileDownloadTask.TaskSnapshot taskSnapshot) {
                            //Some math to get the Percentage of the Download :)
                            double progressPercentage = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                            double size = (taskSnapshot.getTotalByteCount()) / (1000000);
                            progressDialog.setMessage("PDF Size: " + (size) + " - " + ((int) progressPercentage) + "% - Click away to cancel the download.");

                        }
                    });
                }
            }
        });
like image 518
Kishan Nareshpal Avatar asked Jan 31 '17 07:01

Kishan Nareshpal


People also ask

How do I delete files from Firebase storage?

To delete a file, first create a reference to that file. Then call the delete() method on that reference, which returns a Promise that resolves, or an error if the Promise rejects.

How do I download from Firebase storage to Android?

Download to a local fileThe getFile() method downloads a file directly to a local device. Use this if your users want to have access to the file while offline or to share the file in a different app. getFile() returns a DownloadTask which you can use to manage your download and monitor the status of the download.

How do I enable CORS in firebase storage?

CORS Configuration To download data directly in the browser, you must configure your Cloud Storage bucket for cross-origin access (CORS). This can be done with the gsutil command line tool, which you can install from here. Run gsutil cors set cors. json gs://<your-cloud-storage-bucket> to deploy these restrictions.


1 Answers

pdfRef.[getFile][1](localFile) returns a FileDownloadTask. This object is a subclass of CancellableTask, which has a cancel() method. You will need to hold a reference to this task and call its cancel method to cancel the download.

like image 51
Doug Stevenson Avatar answered Sep 21 '22 10:09

Doug Stevenson