0

我的吐司没有出现,我很困惑,因为我认为我做对了,我不明白。正如您所看到的,它是一个显示三个按钮的程序,第一个按钮会激活下载,该下载应该会自动打开 PDF 文件 - 如果没有 pdf 阅读器,应该会弹出一个 toast 说没有阅读器。其他按钮进入新屏幕并用作后退按钮。

public class myactivity extends Activity {

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private Button startBtn;
private ProgressDialog mProgressDialog;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myactivity);
    mProgressDialog = new ProgressDialog(myactivity.this);
    mProgressDialog.setMessage("Please be patient, file downloading...");
    mProgressDialog.setIndeterminate(false);
    mProgressDialog.setMax(100);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

    Button Section = (Button) findViewById(R.id.Section);
    Section.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            // TODO Auto-generated method stub
            Intent Section = new Intent(view.getContext(),
                    Section.class);
            startActivity(Section);

        }
    });

    Button Back = (Button) findViewById(R.id.Back);
    Back.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            setResult(RESULT_OK);
            finish();
        }
    });

    startBtn = (Button) findViewById(R.id.Search);
    startBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            startDownload();
        }
    });
}

private void startDownload() {
    DownloadFile downloadFile = new DownloadFile();
    downloadFile
            .execute("http://www.website.com/document.pdf");

}

class DownloadFile extends AsyncTask<String, Integer, String> {

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

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

    @Override
    protected String doInBackground(String... aurl) {
        try {

            URL url = new URL(aurl[0]);
            URLConnection connection = url.openConnection();

            connection.connect();
            int fileLength = connection.getContentLength();
            int tickSize = 2 * fileLength / 100;
            int nextProgress = tickSize;

            Log.d(

            "ANDRO_ASYNC", "Lenght of file: " + fileLength);

            InputStream input = new BufferedInputStream(url.openStream());

            String path = Environment.getExternalStorageDirectory()
                    + "/Android/Data/"
                    + getApplicationContext().getPackageName() + "/files";
            File file = new File(path);
            file.mkdirs();
            File outputFile = new File(file, "test1.pdf");

            OutputStream output = new FileOutputStream(outputFile);

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

            output.flush();
            output.close();
            input.close();
            showPdf();

        } catch (Exception e) {
        }
        return null;
    }

}

private void showPdf() {
    // TODO Auto-generated method stub
    mProgressDialog.dismiss();

    File file = new File(Environment.getExternalStorageDirectory()
            + "/Android/Data/" + getApplicationContext().getPackageName()
            + "/files/test1.pdf");
    Intent testIntent = new Intent(Intent.ACTION_VIEW);
    testIntent.setType("application/pdf");
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(file);
    intent.setDataAndType(uri, "application/pdf");
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(myactivity .this,
                "No Application Available to View PDF", Toast.LENGTH_LONG).show();
    }

}

}
4

1 回答 1

1
 try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(myactivity .this,
                "No Application Available to View PDF", Toast.LENGTH_LONG).show();
    }

上面的代码仅在您没有在清单文件中声明活动时才显示 toast,但在 pdf 不存在时不显示。您必须FileNotFoundException根据您的要求使用。

更新:::

try{
 File file = new File(Environment.getExternalStorageDirectory()
            + "/Android/Data/" + getApplicationContext().getPackageName()
            + "/files/test1.pdf");
    Intent testIntent = new Intent(Intent.ACTION_VIEW);
    testIntent.setType("application/pdf");
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(file);
    intent.setDataAndType(uri, "application/pdf");
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(myactivity .this,
                "No Application Available to View PDF", Toast.LENGTH_LONG).show();
    }
} catch (FileNotFoundException e) {
        Toast.makeText(myactivity .this,
                "No Application Available to View PDF", Toast.LENGTH_LONG).show();
    }
于 2012-05-05T03:17:29.157 回答