【问题标题】:android downloading two files on single click and share their progress in single progressbarandroid单击下载两个文件并在单个进度条中分享他们的进度
【发布时间】:2012-05-11 16:31:31
【问题描述】:

我从 url 获取一些音频文件及其映射的字幕。现在我想通过单击下载按钮下载该 .mp3 文件和 .srt 文件,并且它们应该共享相同的进度条并显示总进度在相同的。 他们应该保存在sdcard的两个不同目录中。我已经实现了带有进度条的单个文件下载。任何帮助将不胜感激!

谢谢。

更新:所有仍在寻找此类的人的答案...

    String strGetFName;
    String strVipEpub;

    public DownloadFile()
    {
        strGetFName = getFilename();
        strVipEpub = getVipLessonEpubFilename();

    }

    @Override
    protected String doInBackground(String... sUrl) 
    {
        try 
        {
            URL url = new URL(sUrl[0]);
            URL urlePub = new URL(sUrl[1]);
            Log.e("urls"," "+sUrl[0]+","+sUrl[1]);
            URLConnection connection = url.openConnection();
            URLConnection connectionePub = urlePub.openConnection();
            Log.e("connection","done");
            connection.connect();
            connectionePub.connect();
            Log.e("connection","Connected");
            // this will be useful so that you can show a typical 0-100% progress bar
            int fileLength = connection.getContentLength();
            int fileLengthePub = connectionePub.getContentLength();
            Log.e("fileLength"," "+fileLength);
            int bytesFromMP3 = fileLength;
            int bytesFromSubtitle = fileLengthePub;
            int totalBytesToDownload = bytesFromMP3 + bytesFromSubtitle;
            Log.e("bytesFromMP3"," "+bytesFromMP3);
            Log.e("bytesFromSubtitle"," "+bytesFromSubtitle);
            Log.e("total"," "+totalBytesToDownload);

            int bytesDownloadedSoFar = 0;
            while(bytesDownloadedSoFar <= bytesFromSubtitle)
            {
                // download the file
                InputStream input = new BufferedInputStream(urlePub.openStream());

                OutputStream output = new FileOutputStream(strVipEpub);

                byte data[] = new byte[1024];
                long total = 0;
                int count;
                while ((count = input.read(data)) != -1) 
                {
                    total += count;
                    bytesDownloadedSoFar += count;
                    // publishing the progress....
                    publishProgress((int) (bytesDownloadedSoFar * 100 / totalBytesToDownload));
                    output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();
            }
            while(bytesDownloadedSoFar <= bytesFromMP3)
            {
                // download the file
                InputStream input = new BufferedInputStream(url.openStream());

                OutputStream output = new FileOutputStream(strGetFName);

                byte data[] = new byte[1024];
                long total = 0;
                int count;
                while ((count = input.read(data)) != -1) 
                {
                    total += count;
                    bytesDownloadedSoFar += count;
                    // publishing the progress....
                    publishProgress((int) (bytesDownloadedSoFar * 100 / totalBytesToDownload));
                    output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();
            }
        }
        catch (Exception e) 
        {
            Log.e("File"," Not Downloaded"+e.getMessage());
        }
        return null;
    }
    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) 
    {
        super.onProgressUpdate(progress);
        pbDownloading.setProgress(progress[0]);
        tvProgress.setText(progress[0].toString()+"%");
    }

    @Override
    protected void onPostExecute(String result) 
    {
        super.onPostExecute(result);
        Log.e("is ","Downloading complete");
    }

【问题讨论】:

    标签: android download progress-bar share


    【解决方案1】:

    下载多个文件

    如果您有单个下载工作,它应该真的像复制代码并更改它以下载 .srt 文件一样简单。如果您设置了 AsyncTask,您可以将它们设置为一个接一个地下载。

    结合进度条状态

    为此,我猜在开始下载任一文件之前,您需要获取每个文件的大小。将进度条设置为两种大小的总和,然后在下载时使用下载的字节数更新进度条。当您下载下一个文件时,当前的下载值必须继续。

    以下是一些非常粗略的伪代码。我不知道您如何下载文件以及如何处理它(希望它类似于 AsyncTask)。以下只是我将应用的逻辑。如果您到目前为止向我们展示您的代码,我们可以更具体。

    int bytesFromMP3 = GetSizeOfFileAtURL(urlOfMP3File);
    int bytesFromSubtitle = GetSizeOfFileAtURL(urlOfSubtitlesFile);
    int totalBytesToDownload = bytesFromMP3 + bytesFromSubtitle;
    
    progressBar.setMax(totalBytesToDownload);
    
    int bytesDownloadedSoFar = 0;
    
    //Start some loop to download the MP3 File
    while(...) 
    {
        //Download some data from the MP3 File
        ...
    
        //Update the number of bytes downloaded based on the size of the chunk you just downloaded
        bytesDownloadedSoFar += sizeOfChunkJustDownloaded
    
        //Update the progress bar
        progressBar.setProgress(bytesDownloadedSoFar);
    }
    
    //Repeat the loop, but this time download the subtitle info. Don't reset bytesDownloadedSoFar, just continue to add to it. 
    while(...)
    

    【讨论】:

    • 现在正在工作,我已经实现了你的想法,它对我有用......谢谢
    • 太好了,您能否将此问题标记为已回答?谢谢。
    • @Gophermofur 你能告诉我如何使用phonegap android做同样的事情吗?这里是代码stackoverflow.com/q/20510524/2307391
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 2013-11-21
    • 2013-09-18
    • 1970-01-01
    相关资源
    最近更新 更多