0

I have an android app which downloads a video by clicking a button and saves it on user's device. when users click on the button the app checks if the video is in users' device, and if it is not it downloads the video and if it has been downloaded the app just plays the video without the need to download.
However when the download crashes the file is there , but the app can't plays it and gets tricked that the file has been downloaded already.
I wanted to ask if there are any ways to check if the download process has crashed or the file is corrupted.
Thanks

m0j1
  • 4,067
  • 8
  • 31
  • 54

1 Answers1

1

You can use AsyncTask as below to check if the download was interupted or not. Below code is just for example purpose

  class DownloadFileFromURL extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        initialText.setText(getString(R.string.connecting));
    }

    @Override
    protected String doInBackground(String... f_url) {

        int count;
        try {
            String fileURL = f_url[0];
            if (fileURL.contains(" ")) {
                fileURL = fileURL.replace(" ", "%20");
            }
            URL url = new URL(fileURL);

            filename = f_url[0].substring(fileURL.lastIndexOf("/") + 1);


            URLConnection connection = url.openConnection();
            connection.connect();
            // getting file length
            int lengthOfFile = connection.getContentLength();

            // input stream to read file - with 8k buffer
            InputStream input = new BufferedInputStream(url.openStream(), 8192);


            File file = new File(FilePath);
            if (!file.exists()) {
                if (!file.isDirectory()) {
                    file.mkdirs();
                }
            }

            // Output stream to write file
            OutputStream output = new FileOutputStream(FilePath + filename);

            byte data[] = new byte[1024];

            long total = 0;

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    setSubTitle(getString(R.string.downloading));
                    initialText.setText(ad_tv_initialText.getText().toString() + getString(R.string.connected));
                }
            });

            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                // After this onProgressUpdate will be called
                publishProgress("" + (int) ((total * 100) / lengthOfFile));

                // writing data to file
                output.write(data, 0, count);
            }

            // flushing output
            output.flush();

            // closing streams
            output.close();
            input.close();

            return "SUCCESS";

        } catch (Exception e) {
            return "FAILED";
        }
    }

    protected void onProgressUpdate(String... progress) {
        progress.setProgress(Integer.parseInt(progress[0]));
        progressText.setText(progress[0] + " %");
    }

    @Override
    protected void onPostExecute(String result) {
        if (result.equals("SUCCESS")) {
            takeDecisionToPlay();
        } else if (result.equals("FAILED")) {
            setSubTitle(getString(R.string.failed_));
            finalText.setText(getString(R.string.failed));
        }
    }
}
Ankii Rawat
  • 2,070
  • 17
  • 17