完全下载之前显示的文件

时间:2016-09-26 05:26:24

标签: android android-asynctask download

我的任务是从服务器下载mp4文件并将其显示在循环中。我正在使用ION downloader下载文件。以下是我到目前为止尝试的代码: -

for (int i = 0; i < mainModel.size(); i++) {
    downloadFile(mainModel.get(i));
}

private void downloadFile(final FileModel model) {

    new DownloadFileTask(FilesActivity.this, model, new DownloadFileTask.Callback() {

        @Override
        public void onDownloadComplete(File result) {

            if (result != null) {

                listFiles.add(result);

                if (listFiles.size() == mainModel.size()) {
                    Log.e("Downloading", "Complete");
                    startTimer();
                }
            }
        }

        @Override
        public void onError(Exception e) {
            Log.e(TAG, "Failed to download file.", e);
            Toast.makeText(FilesActivity.this, "An error has occurred", Toast.LENGTH_SHORT).show();
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }).execute();
}
  

DownloadFileTask.java

class DownloadFileTask extends AsyncTask<Void, Void, File> {

    private final Context mContext;
    private final Callback mCallback;
    private Exception mException;
    private FileModel fileModel;
    long total = 0;
    boolean download = false;

    DownloadFileTask(Context context, FileModel fileModel, Callback callback) {
        mContext = context;
        mCallback = callback;
        this.fileModel = fileModel;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected File doInBackground(Void... params) {

        /*FileMetadata metadata = params[0];
        long sizeOnServer = metadata.getSize();*/

        try {
            String fileName = "";
            String url = "";

            fileName = fileModel.getFile_name() + ".mp4";
            url = fileModel.getVideo();

            File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            File file = new File(path, fileName);

            /* Make sure the Downloads directory exists. */
            if (!path.exists()) {
                if (!path.mkdirs()) {
                    mException = new RuntimeException("Unable to create directory: " + path);
                }
            } else if (!path.isDirectory()) {
                mException = new IllegalStateException("Download path is not a directory: " + path);
                return null;
            }

            /* Download the file. */
            if (!file.exists()) {

                Ion.with(mContext)
                        .load(url)
                        .progress(new ProgressCallback() {
                            @Override
                            public void onProgress(long downloaded, long total) {
                                Log.e("Downloading", "" + downloaded + " / " + total);
                            }
                        })
                        .write(new File(path, fileName))
                        .setCallback(new FutureCallback<File>() {
                            @Override
                            public void onCompleted(Exception e, File file) {
                                mCallback.onDownloadComplete(file);
                            }
                        });

            }

            /* Tell android about the file */
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            intent.setData(Uri.fromFile(file));
            mContext.sendBroadcast(intent);

            return file;

        } catch (Exception e) {
            mException = e;
        }

        return null;
    }

    @Override
    protected void onPostExecute(File result) {
        super.onPostExecute(result);
        if (mException != null) {
            mCallback.onError(mException);
        } else {
            mCallback.onDownloadComplete(result);
        }
    }

    public interface Callback {
        void onDownloadComplete(File result);
        void onError(Exception e);
    }

}

但问题是我的循环在所有文件下载之前就开始了。请求给我一些关于这个问题解决方案的建议。

1 个答案:

答案 0 :(得分:1)

我解决了这个问题,感谢@Murtaza Khursheed Hussain

int downloadFlag = 0;

if (mainModel.size() > 0) {
    downloadFile(mainModel.get(downloadFlag));
}

private void downloadFile(final FileModel model) {
    new DownloadFileTask(TabRideActivity.this, model, new DownloadFileTask.Callback() {
        @Override
        public void onDownloadComplete(File result) {

            if (result != null) {

                listFiles.add(result);
                downloadFlag++;

                if (downloadFlag < mainModel.size()) {
                    downloadFile(mainModel.get(downloadFlag));
                    Log.e("Download", "" + listFiles.size());
                    Log.e("Download", "" + mainModel.size());
                } else {
                    Toast.makeText(TabRideActivity.this, "Download Finished", Toast.LENGTH_SHORT).show();
                    Log.e("Download", "Finished");
                }

            }
        }
    }).execute();
}