2

我正在使用以下代码从 pdf 列表中下载 pdf,具体取决于所选的内容。我想然后打开下载的pdf。问题是打开pdf的代码发生在下载完成之前。如何使打开pdf的代码在下载完成之前不会运行.....

注意:我最初以文本/html 格式阅读 pdf 的原因是因为我最初将 pdf 作为网站 url,然后在 url 中打开时自动下载。

  public class pdfSelectedListener implements OnItemClickListener{

    @Override
    public void onItemClick(AdapterView<?> parent,
            View view, int pos, long id) {
        String pdfName = "";

        for(int i=0;i<nameList.size();i++){
            if(nameList.get(i).equals(parent.getItemAtPosition(pos).toString())){
                try{
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.parse(websiteList.get(i)), "text/html");


                int slashIndex = websiteList.get(i).lastIndexOf('/');
                pdfName = websiteList.get(i).substring(slashIndex+1, websiteList.get(i).length());

                startActivity(intent);
                }catch(Exception e){
                    Toast.makeText(PDFActivity.this, "Invalid link.", Toast.LENGTH_LONG).show();
                }
            }
        }

//在上面的代码完成从互联网下载pdf之前,我不希望下面的代码执行。

                    File file = new File("/mnt/sdcard/Download/"+pdfName);
                        if (file.exists()) {
                            Uri path = Uri.fromFile(file);
                            Intent intent = new Intent(Intent.ACTION_VIEW);
                            intent.setDataAndType(path, "application/pdf");
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                            try {
                                startActivity(intent);
                            } 
                            catch (ActivityNotFoundException e) {
                                Toast.makeText(PDFActivity.this, 
                                    "No Application Available to View PDF", 
                                    Toast.LENGTH_SHORT).show();
                            }
                        }else{
                            Toast.makeText(PDFActivity.this, 
                                    "File doesn't exist.", 
                                    Toast.LENGTH_SHORT).show();
                        }
        }
    }   
4

2 回答 2

1

使用 AsyncTask 并在对话框中显示下载进度

// declare the dialog as a member field of your activity
ProgressDialog mProgressDialog;

// instantiate it within the onCreate method
mProgressDialog = new ProgressDialog(YourActivity.this);
mProgressDialog.setMessage("A message");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

// execute this when the downloader must be fired
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("the url to the file you want to download");

AsyncTask 将如下所示:

private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
    try {
        URL url = new URL(sUrl[0]);
        URLConnection connection = url.openConnection();
        connection.connect();
        // this will be useful so that you can show a typical 0-100% progress bar
        int fileLength = connection.getContentLength();

        // download the file
        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream("/sdcard/file_name.extension");

        byte data[] = new byte[1024];
        long total = 0;
        int count;
        while ((count = input.read(data)) != -1) {
            total += count;
            // publishing the progress....
            publishProgress((int) (total * 100 / fileLength));
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {
    }
    return null;
}

上面的方法(doInBackground)总是在后台线程上运行。你不应该在那里做任何 UI 任务。另一方面,onProgressUpdate 和 onPreExecute 在 UI 线程上运行,因此您可以更改进度条:

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

@Override
protected void onProgressUpdate(Integer... progress) {
    super.onProgressUpdate(progress);
    mProgressDialog.setProgress(progress[0]);
}

}

如需进一步参考,请查看链接下载文件和显示进度的可能方法

于 2012-06-25T13:21:36.963 回答
1

您应该实现AsyncTask下载 PDF 文件。

  • 在 doInBackground() 中,下载 PDF 文件
  • 在 onPostExecute() 中,为下载的 PDF 做任何你想做的事情。
于 2012-06-25T13:08:56.990 回答