2

次のコードを使用して、選択したPDFのリストからPDFをダウンロードしています。ダウンロードしたPDFを開きたいです。問題は、ダウンロードが完了する前にPDFを開くためのコードが発生することです。ダウンロードが完了するまでPDFを開くコードが実行されないようにするにはどうすればよいですか.....

注:PDFを元々text / htmlとして読んでいる理由は、元々PDFをWebサイトの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

AsyncTaskPDFファイルをダウンロードするために実装する必要があります。

  • doInBackground()内で、PDFファイルをダウンロードします
  • onPostExecute()内で、ダウンロードしたPDFに対して実行したいことをすべて実行します。
于 2012-06-25T13:08:56.990 に答える
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 に答える