如何检查文件是从 Firebase 完全下载还是在 android 中已损坏?

问题描述

我正在使用一个应用程序,我正在从 firebase 存储下载 pdf 文件。如果互联网连接稳定,则文件已成功下载。但是当下载过程中互联网连接中断时,文件会损坏,我无法再次开始下载过程。而且我也无法检查下载的文件是否已损坏 我的代码

    File storagePath = new 
  File(getExternalFilesDir("quranfolder").getAbsolutePath());
  // Create direcorty if not exists
    if (!storagePath.exists()) {
        storagePath.mkdirs();

        myFile = new File(storagePath,QuranFile);
    }
    if (myFile == null) {

        myFile = new File(storagePath,QuranFile);
    }

    if (myFile.exists()) {
        //do stuff
        displayFile();
    } else {
        ProgressDialog pd = new ProgressDialog(this);
        pd.setTitle("Quran");
        pd.setMessage("Downloading Please Wait!");
        pd.setIndeterminate(true);
        pd.setCancelable(false);
        pd.setProgressstyle(ProgressDialog.STYLE_SPINNER);
        pd.show();
        // Create direcorty if not exists
        if (!storagePath.exists()) {
            storagePath.mkdirs();
        }
        final File myFile = new File(storagePath,QuranFile);


        //String destPath = mContext.getExternalFilesDir(null).getAbsolutePath();

        firebaseStorage = FirebaseStorage.getInstance();

        storageReference = firebaseStorage.getReference();
        //  ref=storageReference.child("quranrangeen1.pdf");
        ref = storageReference.child(QuranFile);

        ref.getFile(myFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                // Local temp file has been created
                System.out.println("error no mi");
                pd.dismiss();
                displayFile();



            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                System.out.println("error nomi" + exception.getMessage());
                // Handle any errors
                   pd.dismiss();
            }
        }).addOnProgressListener(new OnProgressListener<FileDownloadTask.TaskSnapshot>() {
            @Override
            public void onProgress(FileDownloadTask.TaskSnapshot taskSnapshot) {
                //calculating progress percentage
                double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                //displaying percentage in progress dialog
                pd.setMessage("Downloaded " + ((int) progress) + "%...");
            }
        });

如何检查之前下载的文件是否损坏?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)