【问题标题】:facing problem unable to download files in a que using async面临问题无法使用异步下载队列中的文件
【发布时间】:2022-10-19 21:22:19
【问题描述】:

我通过创建 Async 类来处理这个问题,然后将 URL 列表传递给该类。在 Async 类内部,循环下载队列中的文件

【问题讨论】:

    标签: java android android-studio kotlin asynchronous


    【解决方案1】:
    class DownloadFileAsync extends AsyncTask<ArrayList<String>, String, String>
     {
    
           int index = 0;
    
            @Override
            protected String doInBackground(ArrayList<String>... arrayLists) {
    
                int count;
    
                for (String fileUrl : allFileUrls) {
                    ++index;
                    String fileName = fileUrl.substring(fileUrl.lastIndexOf('/') + 1, fileUrl.length());
                    File dir = new File(getFilesDir().getAbsolutePath() + "/" + fileName);
    
                    if (!dir.exists()) {
    
                        try {
                            URL url = new URL(fileUrl);
                            URLConnection conexion = url.openConnection();
                            conexion.connect();
    
                            int lenghtOfFile = conexion.getContentLength();
    
    
                            InputStream input = new BufferedInputStream(url.openStream());
                            OutputStream output = new FileOutputStream(dir);
    
                            byte data[] = new byte[1024];
    
                            long total = 0;
    
                            while ((count = input.read(data)) != -1) {
                                total += count;
                                publishProgress("" + (int) ((total * 100) / lenghtOfFile), String.valueOf(index));
                                output.write(data, 0, count);
                            }
    
                            output.flush();
                            output.close();
                            input.close();
                        } catch (Exception e) {
                            Log.d("ANDRO_ASYNC error", e.getMessage());
    
                        }
    
                    }
                }
    
    
                return null;
            }
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(SyncActivity.this);
                pDialog.setTitle("Please wait");
                pDialog.setMessage("Starting download..");
                pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                pDialog.setCancelable(false);
                pDialog.show();
            }
    
            protected void onProgressUpdate(String... progress) {
    
                pDialog.setMessage(" Downloading the File : " + index);
                pDialog.setProgress(Integer.parseInt(progress[0]));
    
            }
    
    
            @Override
            protected void onPostExecute(String unused) {
                pDialog.dismiss();
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2011-11-14
      • 2019-10-17
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      相关资源
      最近更新 更多