Android,检查文件传输是否成功

时间:2015-09-24 18:51:48

标签: android download

我有一个Android应用程序,通过单击按钮下载视频并将其保存在用户的设备上。当用户点击按钮时,应用会检查视频是否在用户中。设备,如果不是,它会下载视频,如果已经下载,应用程序只需播放视频而无需下载。
但是,当下载崩溃时,文件就在那里,但应用程序无法播放该文件并被欺骗,因为该文件已经下载。
我想询问是否有任何方法可以检查下载过程是否已崩溃或文件是否已损坏。
感谢

1 个答案:

答案 0 :(得分:1)

您可以使用以下AsyncTask来检查下载是否已中断。 下面的代码仅用于示例目的

  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));
        }
    }
}