ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Firebase Android:如何取消下载?

2019-11-11 20:26:00  阅读:342  来源: 互联网

标签:firebase download firebase-storage android


如何取消从Firebase的下载任务?

每当我单击ProgressDialog上的某个位置时,我都想取消下载.

这是我的下载活动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)返回FileDownloadTask.此对象是CancellableTask的子类,它具有cancel()方法.您将需要保留对此任务的引用,并调用其cancel方法来取消下载.

标签:firebase,download,firebase-storage,android
来源: https://codeday.me/bug/20191111/2022253.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有