安卓端使用线程下载文件卡滞

需求:从阿里云oss下载大量的图片(4000+)到手机的内部存储

  /// <summary>
    /// 指定要下载的文件列表
    /// </summary>
    /// <param name="aliyunFile">阿里云上要下载文件所在文件夹</param>
    /// <param name="saveFile">下载文件存储文件夹(Application.streamingAssetsPath下)</param>
    /// <param name="completedCallback">下载完成回调事件</param>
    /// <param name="needUI">下载过程中是否需要展示UI</param>
    public void StartDownloadFileList(List<string> downloadFileList, string saveFile, Action completedCallback = null, bool needUI = true)
    {
        if (NetworkStatus.IsNetNotReachable())
        {
            TipsManager.Instance.ShowTip("当前设备网络未连接,请检查网络");
            return;
        }

        Debug.Log(TAG + "StartDownloadFileList by list");
        if (needUI)
        {
            StatusText.text = "资源下载中...";
        }
        curDownloadIndex = lastDownloadIndex = 0;
        if (completedCallback != null)
        {
            DownloadCompletedCallback = completedCallback;
        }
        ShowDownloadUI = needUI;
        allFileList = downloadFileList;
        if (allFileList.Count > 0)
        {
            DownloadSingleObj(saveFile, allFileList[curDownloadIndex]);
        }
    }


 /// <summary>
    /// 下载单个文件
    /// </summary>
    /// <param name="targetFile">本地存储路径文件夹</param>
    /// <param name="filePath">oss上文件存储路径</param>
    /// <param name="savePath">直接指定的存储路径</param>
    public void DownloadSingleObj(string targetFile, string filePath, string savePath = "")
    {
        Debug.Log("下载单个文件: " + filePath);
        string dir = StringTranslation.GetStoragePath() + "/" + targetFile + "/" + filePath.Substring(0, filePath.LastIndexOf("/"));
        thread = new Thread(GetObject);
        if (string.IsNullOrEmpty(savePath))
        {
            FileHelper.CreateDirectory(dir);
            GetObjectByThread(targetFile, filePath, StringTranslation.GetStoragePath() + "/" + targetFile + "/" + filePath);
        }
        else
        {
            GetObjectByThread(targetFile, filePath, savePath);
        }
    }

  private void GetObjectByThread(string targetFile, string filePath, string savePath)
    {
        this.targetFile = targetFile;
        this.filePath = filePath;
        this.savePath = savePath;
        //thread = new Thread(GetObject);
        thread.Start();
    }

 private void GetObject()
    {
        try
        {
            GetObjectRequest getObjectRequest = new GetObjectRequest(AliyunConfig.BucketName, filePath);
            getObjectRequest.StreamTransferProgress += StreamProcess;
            OssObject result = client.GetObject(getObjectRequest);
            using (var resultStream = result.Content)
            {
                string directory = savePath.Substring(0, savePath.LastIndexOf("/"));
                FileHelper.CreateDirectory(directory);

                using (var fs = File.Open(savePath, FileMode.OpenOrCreate))
                {
                    int length = (int)resultStream.Length;
                    byte[] bytes = new byte[length];
                    do
                    {
                        length = resultStream.Read(bytes, 0, length);
                        fs.Write(bytes, 0, length);

                    } while (length != 0);
                }
                print(string.Format("第{0}个下载成功 。", curDownloadIndex + 1));

                if (curDownloadIndex < allFileList.Count - 1)
                {
                    {
                        curDownloadIndex++;
                    }
                    DownloadSingleObj(targetFile, allFileList[curDownloadIndex]);
                }
                else
                {
                    Loom.QueueOnMainThread(() =>
                    {
                        print("所有文件下载完毕 " + (curDownloadIndex + 1));
                        if (ShowDownloadUI)
                        {
                            StatusText.text = "资源下载完毕";
                        }
                        DownloadCompletedCallback?.Invoke();
                        DownloadCompletedCallback = null;
                        CancelInvoke("CheckDownloadProcess");
                    });
                    thread.Abort();
                }
            }
        }
        catch (OssException e)
        {
            print("进度下载文件出错:" + e.Message);
        }
    }

问题:下载一部分之后,下载进度会停止;

解决办法: 检测下载进度是否停止,如果停止,则重启线程继续下载;

    IEnumerator RestartThread()
    {
        thread.Abort();
        yield return new WaitForSeconds(Time.deltaTime);
        thread = new Thread(GetObject);
        thread.Start();
        DownloadSingleObj(targetFile, allFileList[curDownloadIndex]);
    }

    InvokeRepeating("CheckDownloadProcess", 0, 3);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值