Android:下载后打开PDF格式文件

  1.         File file = new File("/sdcard/example.pdf");

  2.                 if (file.exists()) {
  3.                     Uri path = Uri.fromFile(file);
  4.                     Intent intent = new Intent(Intent.ACTION_VIEW);
  5.                     intent.setDataAndType(path, "application/pdf");
  6.                     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

  7.                     try {
  8.                         startActivity(intent);
  9.                     } 
  10.                     catch (ActivityNotFoundException e) {
  11.                         Toast.makeText(OpenPdf.this, 
  12.                             "No Application Available to View PDF", 
  13.                             Toast.LENGTH_SHORT).show();
  14.                     }
  15.                 }
上面的可以正常打开的



  1. Uri path = Uri.fromFile(file);  
  2.                         Intent intent = new Intent(Intent.ACTION_VIEW);  
  3.                         intent.setDataAndType(path, "application/pdf");  
  4.                         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  5.   
  6.   
  7.                         try {  
  8.                                 startActivity(intent);  
  9.                         } catch (ActivityNotFoundException e) {  
  10.                                 Toast.makeText(this,  
  11.                                                 "No Application Available to View PDF",  
  12.                                                 Toast.LENGTH_SHORT).show();  
  13.                         }  


http://blog.csdn.net/xiaojunhu/article/details/8814710

http://blog.csdn.net/ouyangtianhan/article/details/6975899

首先是下载,不再赘述。

然后定义打开pdf的Intent

[java]  view plain copy
  1. /** 
  2.     * Get PDF file Intent 
  3.     */  
  4.    public Intent getPdfFileIntent(String path){  
  5.     Intent i = new Intent(Intent.ACTION_VIEW);  
  6.     i.addCategory(Intent.CATEGORY_DEFAULT);  
  7.     i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );  
  8.     Uri uri = Uri.fromFile(new File(path));  
  9.     i.setDataAndType(uri, "application/pdf");  
  10.     return i;  
  11. }  

打开

[java]  view plain copy
  1. btnDownload.setOnClickListener(new View.OnClickListener() {  
  2.             public void onClick(View v) {  
  3.                 //You need to open another thread to download file ,  
  4.                 //so that it cannot disturb the main UI thread.  
  5.                 final HttpDownloader loader = new HttpDownloader(getApplicationContext());  
  6.                 int result = loader.download("http://xxx.pdf""Tian/""t0.pdf");  
  7.                 Log.d("PDFActivity.java","Download result: "+result);  
  8.                 if(result == 0 || result == 1){ // Success,open it  
  9.                     Intent intent = getPdfFileIntent("/mnt/sdcard/Tian/t0.pdf");  
  10.                     startActivity(intent);  
  11.                 }  
  12.             }  
  13.         });  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值