0

我的 android 应用程序在下载文件时总是卡住,直到下载完成。但是,下载线程是从 AyncTask 继承的,它在后台。任何人都可以看看有什么问题,我该如何修改代码以使其正常工作?

    private class DownloadFileTask extends AsyncTask<String, Integer, String> {

    File destFile;

    private boolean openAfterDownload;
    private Exception failure;

    public DownloadFileTask(boolean openAfterDownload) {
        this.openAfterDownload = openAfterDownload;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();           
        downloadDialog.setMessage(getString(R.string.downloading));
        downloadDialog.show();
    }

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

        try {

            String url = params[0];
            LOG.debug("Downloading: " + url);

            String fileName = url.substring(url.lastIndexOf('/') + 1);

            HttpParams httpParams = new BasicHttpParams();
            DefaultHttpClient client = new DefaultHttpClient(httpParams);
            client.getCredentialsProvider().setCredentials(
                    new AuthScope(null, -1),
                    new UsernamePasswordCredentials(user, password));
            HttpGet get = new HttpGet(url);

            HttpResponse response = client.execute(get);

            if (response.getStatusLine().getStatusCode() == 200) {

                File destFolder = new File(config.getDownloadsFolder());
                if (!destFolder.exists()) {
                    destFolder.mkdirs();
                }

                /**
                 * Make sure we always store downloaded files as .epub, 
                 * so they show up in scans later on.
                 */
                if ( ! fileName.endsWith(".epub") ) {
                    fileName = fileName + ".epub";
                }

                destFile = new File(destFolder, URLDecoder.decode(fileName));

                if (destFile.exists()) {
                    destFile.delete();
                }

                // lenghtOfFile is used for calculating download progress
                long lenghtOfFile = response.getEntity().getContentLength();
                if(lenghtOfFile>=config.getAvailableSpace())
                {
                    this.failure = new Exception("not enough space");
                    return null;
                }
                // this is where the file will be seen after the download
                FileOutputStream f = new FileOutputStream(destFile);

                try {
                    // file input is from the url
                    InputStream in = response.getEntity().getContent();

                    // here's the download code
                    byte[] buffer = new byte[1024];
                    int len1 = 0;
                    long total = 0;

                    while ((len1 = in.read(buffer)) > 0) {

                        // Make sure the user can cancel the download.
                        if (isCancelled()) {
                            return null;
                        }

                        total += len1;
                        publishProgress((int) ((total * 100) / lenghtOfFile));
                        f.write(buffer, 0, len1);
                    }
                } finally {
                    f.close();
                }

            } else {
                this.failure = new RuntimeException(response
                        .getStatusLine().getReasonPhrase());
                LOG.error("Download failed: "
                        + response.getStatusLine().getReasonPhrase());
            }

        } catch (Exception e) {
            Toast.makeText(getActivity(), e.getMessage() + "1",
                    Toast.LENGTH_LONG).show();
            LOG.error("Download failed.", e);
            this.failure = e;
        }

        return null;
    }
4

1 回答 1

0

尽管您正在运行您的任务,AsyncTask但您有这些代码行:

    downloadDialog.setMessage(getString(R.string.downloading));
    downloadDialog.show();

在你的onPreExecute()方法中,它基本上会在它自己开始的任务之前启动一个 DialogBox。

还有什么我onPostExecute()在您的实现中看不到该方法。所以你永远不是.dismiss()对话。但即使你这样做了,在下载完成之前,你的应用程序仍然会在此对话框中“堆叠”(正常行为)。

于 2013-05-14T18:40:33.223 回答