【问题标题】:The best way to download and pause ,resume a big file in android? [duplicate]在android中下载和暂停,恢复大文件的最佳方法? [复制]
【发布时间】:2017-10-07 12:58:15
【问题描述】:

我需要在我的应用程序上下载一个大文件(几乎 1gb),我想知道最好的方法是什么,以及一些可以帮助我的库。首先,我查看了 Android 异步 Http 库,但没有找到显示如何发布进度或开始、暂停下载的示例。然后我不知道我应该使用这个库还是只使用标准的 http commons。另一个问题是,我应该使用服务来下载我的文件吗??

【问题讨论】:

    标签: android asynchronous android-download-manager download-manager


    【解决方案1】:

    您可以在 asynctask 中使用此代码来下载简历:

    @SuppressLint("Wakelock")
        @Override
        protected String doInBackground(String... sUrl) {
            // take CPU lock to prevent CPU from going off if the user 
            // presses the power button during download
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                 getClass().getName());
            wl.acquire();
    
            try {
                InputStream input = null;
                OutputStream output = null;
                HttpURLConnection connection = null;
                try {
                    URL url = new URL(sUrl[0]);
                    connection = (HttpURLConnection) url.openConnection();
                    File SDCardRoot = Environment.getExternalStorageDirectory();                    
                    File file = new File(SDCardRoot,FOLDER_PATH1+FOLDER_PATH2+ "/"+fileName);
                    int downloaded=0;
                    if(file.exists()){
                        downloaded=(int) file.length();
                        connection.setRequestProperty("Range", "bytes=" + (int) file.length() + "-");
                    }
                    else{
                        file.createNewFile();
                    }
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    connection.connect();
    
                    // expect HTTP 200 OK, so we don't mistakenly save error report 
                    // instead of the file
    
    
                    // this will be useful to display download percentage
                    // might be -1: server did not report the length
                    int fileLength = connection.getContentLength()+(int)file.length();
                    // download the file
                    input = connection.getInputStream();      
                    if(downloaded>0){
                        output = new FileOutputStream(file,true);
                    }
                    else{
                        output = new FileOutputStream(file);
                    }
                    byte data[] = new byte[1024];
                    long total = downloaded;
                    int count;
                    mProgressDialog.setMax(fileLength/1024);
                    while ((count = input.read(data)) != -1) {
                        // allow canceling with back button
                        if (isCancelled())
                            return null;
                        total += count;
                        // publishing the progress....
                        if (fileLength > 0) // only if total length is known
                            publishProgress((int)total/1024);
                        output.write(data, 0, count);
                    }
                    output.flush();
                    if (output != null)
                        output.close();
                    if (input != null)
                        input.close();
                    if (connection != null)
                        connection.disconnect();
                    wl.release();
                    return null;
                } catch (Exception e) {
                    return e.toString();
                }
            }
            catch (Exception e) {
                return e.toString();
            }        
        }
    

    【讨论】:

      【解决方案2】:

      您应该使用库,因为它会处理您的所有测试用例并帮助您保持下载状态。

      一些图书馆:

      1]ThinDownloadManager

      2]Android-Download-Manager-Pro

      【讨论】:

        【解决方案3】:

        您应该使用DownLoad Manager,这是一个可以处理长时间运行的 HTTP 下载的系统服务。

        【讨论】:

          猜你喜欢
          • 2021-07-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多