- File file = new File("/sdcard/example.pdf");
-
- 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(OpenPdf.this,
- "No Application Available to View PDF",
- Toast.LENGTH_SHORT).show();
- }
- }
-
- 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(this,
- "No Application Available to View PDF",
- Toast.LENGTH_SHORT).show();
- }
http://blog.csdn.net/xiaojunhu/article/details/8814710
http://blog.csdn.net/ouyangtianhan/article/details/6975899
首先是下载,不再赘述。
然后定义打开pdf的Intent
- /**
- * Get PDF file Intent
- */
- public Intent getPdfFileIntent(String path){
- Intent i = new Intent(Intent.ACTION_VIEW);
- i.addCategory(Intent.CATEGORY_DEFAULT);
- i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
- Uri uri = Uri.fromFile(new File(path));
- i.setDataAndType(uri, "application/pdf");
- return i;
- }
打开
- btnDownload.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- //You need to open another thread to download file ,
- //so that it cannot disturb the main UI thread.
- final HttpDownloader loader = new HttpDownloader(getApplicationContext());
- int result = loader.download("http://xxx.pdf", "Tian/", "t0.pdf");
- Log.d("PDFActivity.java","Download result: "+result);
- if(result == 0 || result == 1){ // Success,open it
- Intent intent = getPdfFileIntent("/mnt/sdcard/Tian/t0.pdf");
- startActivity(intent);
- }
- }
- });