首页 > 解决方案 > 从firebase下载文件时如何显示进度条?

问题描述

我正在尝试设置一个类似于 WhatsApp 的语音邮件,以便用户点击“播放”按钮;该应用程序首先下载文件,然后才允许它播放。下载有效,但我不知道如何集成进度条。

public void Download(){
    int mtn = i++ ;
    final StorageReference ref2 = storageReference.child("/Audio/").child("new_audio0.mp3");
    ref2.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {

            DownLoadFile(context,"new_audio",".mp3",DIRECTORY_DOWNLOADS, uri);
            Log.i("URL_LINK", uri.toString());
            Toast.makeText(context, "En cours de télechargement..", Toast.LENGTH_SHORT).show();

        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(context, "Le telechargement a échoué", Toast.LENGTH_SHORT).show();

        }
    }).addOnCompleteListener(new OnCompleteListener<Uri>() {
        @Override
        public void onComplete(@NonNull Task<Uri> task) {

        }
    });
}

public void DownLoadFile(Context context , String fileName , String fileExtension, String DestinationDirectory, Uri uri ){
    DownloadManager downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);

    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalFilesDir(context,DestinationDirectory, fileName + fileExtension);
    assert downloadManager != null;
    downloadManager.enqueue(request);
}

标签: androidfirebase-storageandroid-download-manager

解决方案


推荐阅读