Firebase Android:如何取消下载?
Firebase Android: How to cancel download?
如何从 Firebase 取消下载任务?
我想在点击 ProgressDialog
以外的某个地方时取消下载。
这是我的下载 Activity ExamesActivity.java
所在的部分。看起来像:
//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.");
}
});
}
}
});
pdfRef.[getFile][1](localFile)
returns一个FileDownloadTask. This object is a subclass of CancellableTask,其中有一个cancel()
方法。您将需要保留对此任务的引用并调用其取消方法来取消下载。
这是我实施的解决方案:
>> 在 OnProgressListener
上,我创建了一个 StorageTask
的 _storageTask
变量键入并存储快照的 task
。此外,使变量可全局访问。
....
StorageTask _storageTask; //global variable
.....
.addOnProgressListener(snapshot -> { _storageTask = snapshot.getTask()});
....
>> 创建了一个方法 cancelUploading()
可以在需要取消上传过程时调用。
private void cancelUploading()
{
//Cancel the uploading process to the firebase storage.
_storageTask.cancel();
_storageTask.addOnCanceledListener(() -> Toast.makeText(AssignAnnounceActivity.this, "Uploading", Toast.LENGTH_SHORT).show());
}
注意:您可以在需要取消上传过程时使用该方法。
如何从 Firebase 取消下载任务?
我想在点击 ProgressDialog
以外的某个地方时取消下载。
这是我的下载 Activity ExamesActivity.java
所在的部分。看起来像:
//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.");
}
});
}
}
});
pdfRef.[getFile][1](localFile)
returns一个FileDownloadTask. This object is a subclass of CancellableTask,其中有一个cancel()
方法。您将需要保留对此任务的引用并调用其取消方法来取消下载。
这是我实施的解决方案:
>> 在 OnProgressListener
上,我创建了一个 StorageTask
的 _storageTask
变量键入并存储快照的 task
。此外,使变量可全局访问。
....
StorageTask _storageTask; //global variable
.....
.addOnProgressListener(snapshot -> { _storageTask = snapshot.getTask()});
....
>> 创建了一个方法 cancelUploading()
可以在需要取消上传过程时调用。
private void cancelUploading()
{
//Cancel the uploading process to the firebase storage.
_storageTask.cancel();
_storageTask.addOnCanceledListener(() -> Toast.makeText(AssignAnnounceActivity.this, "Uploading", Toast.LENGTH_SHORT).show());
}
注意:您可以在需要取消上传过程时使用该方法。