【问题标题】:How to implement multiple downloads with progress bar updation??如何通过进度条更新实现多次下载?
【发布时间】:2012-05-30 13:25:28
【问题描述】:

说明: 我有一个网格视图,横向模式有 4 个项目,纵向模式有 3 个项目。网格元素是杂志缩略图。单击该杂志的缩略图开始下载,并出现一个水平进度条,显示下载进度。但是当我一个接一个地点击两个缩略图时,后来点击的缩略图的进度条只会更新,最后损坏的杂志被下载了。 我使用异步任务来下载杂志。

代码:

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {


    progress = (ProgressBar) arg1.findViewById(R.id.progress);
    thumbnail = (ImageView) arg1.findViewById(R.id.thumbnail);



    thumbnail.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // Download Magazine
             downloadMagazine.execute(bank.get(index).getPdfLink());

            }
    });

}


class DownloadMagazineTask extends AsyncTask<String, Integer, Long> {
    File sdDir = Environment.getExternalStorageDirectory();

    @Override
    protected void onPreExecute() {

       progress.setVisibility(View.VISIBLE);

    }

    @Override
    protected Long doInBackground(String... arg0) {

        String[] urls = arg0;

        try {
            URL url = new URL(urls[0]);
            URLConnection con = url.openConnection();
            int fileLength = con.getContentLength();

            InputStream input = new BufferedInputStream(url.openStream());

            File file = null;

            File dir = new File(sdDir + "/BeSpoken/pdfs");
            boolean flag = dir.mkdirs();
            if (flag)

                System.out.println("Directory created");
            else {
                System.out.println("Directory not created");
                file = new File(dir+ "/"+ bank.get(index).getPdfLink().substring(bank.get(index).getPdfLink().lastIndexOf("/")));

            }
            OutputStream output = new FileOutputStream(file);

            byte data[] = new byte[1024];
            long total = 0;

            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                publishProgress((int) (total * 100 / fileLength));
                output.write(data, 0, count);
            }


            String m = bank.get(index).getTitle();
            manager.updateDownloadedMagazines(
                    Integer.parseInt(m.substring(m.lastIndexOf(" ") + 1)),
                    file.toString());
            output.flush();
            output.close();
            input.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        startActivity(getIntent());
        finish();

        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {

        super.onProgressUpdate(values);
        progress.setProgress(values[0]);

    }

    @Override
    protected void onPostExecute(Long result) {

        super.onPostExecute(result);

        progress.setVisibility(View.INVISIBLE);



    }

}

问题: 如何实现多个下载功能,以便进度条分别显示每个下载的进度?

【问题讨论】:

    标签: android android-asynctask android-gridview android-progressbar


    【解决方案1】:

    我已将图像视图和进度条定义为最终状态,并在单击图像视图时将这些引用发送到杂志下载活动正在进行的 AsyncTask。 AsyncTask 产生多个线程,这就是每次下载的进度可以更新的方式。代码:

       @Override
        public View getView(final int position, View convertView, ViewGroup arg2) {
    
            View grid;
    
            if (convertView == null) {
                grid = new View(context);
                grid = layoutInflater.inflate(item, null);
    
            } else {
                grid = (View) convertView;
            }
    
            final TextView title = (TextView) grid.findViewById(R.id.mgntitle);
            title.setText(bank.get(position).getTitle());
            final ImageView imageView = (ImageView) grid
                    .findViewById(R.id.thumbnail);
            imageView.setImageResource(R.drawable.icon);
            final ProgressBar progress = (ProgressBar) grid
                    .findViewById(R.id.progress);
            final ImageView downloadmark = (ImageView) grid
                    .findViewById(R.id.downloadmark);
            String pdfLink = bank.get(position).getPdfLink();
            String filename = pdfLink.substring(pdfLink.lastIndexOf("/") + 1);
            final File targetDir = new File(fileLocation + filename);
            System.out.println("target file name " + targetDir);
    
            if (new File(fileLocation + filename).exists()) {
                if (!getPrefName(filename).equalsIgnoreCase("NA")) {
                    downloadmark.setVisibility(View.VISIBLE);
                }
            }
    
    
    
            imageView.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    if (!targetDir.exists()) {
                            map.put(bank.get(position).getTitle(), progress);
                            new DoBackgroundTask(GridDisplayActivity.this, bank
                                    .get(position).getPdfLink(), progress,
                                    downloadmark, imageView, position)
                                    .execute();
    
    
                    }
    
    
                }
    
            });
    
            imageView.setImageBitmap(BitmapFactory.decodeByteArray(
                    bank.get(position).getCoverPages(), 0, bank.get(position)
                            .getCoverPages().length));
    
    
            return grid;
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多