5

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.");
 }
 });
 }
 }
 });
AL.
37.9k10 gold badges147 silver badges287 bronze badges
asked Jan 31, 2017 at 7:18

2 Answers 2

4

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.

answered Jan 31, 2017 at 20:16
Sign up to request clarification or add additional context in comments.

1 Comment

Is there also any way to cancel getMetadata call? It returns com.google.android.gms.tasks.Task so no cancel() method is available.
0

Here is the solution I implemented:

>> On the OnProgressListener, I created a _storageTask variable of StorageTask type and stored the snapshot's task. Also, made the variable globally accessible.

....
StorageTask _storageTask; //global variable
..... 
 .addOnProgressListener(snapshot -> { _storageTask = snapshot.getTask()});
....

>> Created a method cancelUploading() which can be called when needed to cancel the uploading process.

private void cancelUploading()
{
 //Cancel the uploading process to the firebase storage.
 _storageTask.cancel();
 _storageTask.addOnCanceledListener(() -> Toast.makeText(AssignAnnounceActivity.this, "Uploading", Toast.LENGTH_SHORT).show());
}

NB: You can use that method whenever you need to cancel uploading process.

answered May 1, 2021 at 10:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.