Android 打开pdf 文件


         public boolean openPdfFile()  //调用第三方软件打开pdf文件

{

Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(PATH));
intent.setDataAndType(uri, "application/pdf");
            try {  
                startActivity(intent);  
            } catch (ActivityNotFoundException e) { 
                Toast.makeText(mActivity, "open WPS failure", Toast.LENGTH_LONG).show();  
                e.printStackTrace();
                return false;  
            } 
return true;
  }

        


    //调用WPS打开pdf文件

    public static final String OPEN_MODE = "OpenMode";
    public static final String SEND_CLOSE_BROAD = "SendCloseBroad";
    public static final String THIRD_PACKAGE = "ThirdPackage";
    public static final String CLEAR_TRACE = "ClearTrace";
    public static final String USER_NAME = "UserName";
    public static final String PATH ="/system/etc/whitelist/readme.pdf";
    
    public static final String PACKAGENAME_ENG = "cn.wps.moffice_eng";  
    public static final String PACKAGENAME_ENT = "cn.wps.moffice_ent";  
    public static final String PACKAGENAME = "cn.wps.moffice";          
    public static final String PACKAGENAME_I18N_TV = "cn.wps.moffice_i18n_TV";  
    public static final String PACKAGENAME_I18N = "cn.wps.moffice_i18n";    
    public static final String CLASSNAME = "cn.wps.moffice.documentmanager.PreStartActivity2";      
    
    public boolean checkPackage(String packageName) 
    {  
        if (packageName == null || "".equals(packageName))  
            return false;  
        try 
        {  
            mActivity.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);  
            return true;  
        } 
        catch (NameNotFoundException e) 
        {  
            return false;  
        }  
    } 
    
    public boolean  openWPS(){        
            Intent intent = new Intent();  
            Bundle bundle = new Bundle();  
            bundle.putString(OPEN_MODE, "Normal"); 
            bundle.putBoolean(SEND_CLOSE_BROAD, true); 
            bundle.putString(THIRD_PACKAGE, mActivity.getPackageName()); 
            bundle.putBoolean(CLEAR_TRACE, true);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
            intent.setAction(android.content.Intent.ACTION_VIEW);
            
            if (checkPackage(PACKAGENAME_ENG)){
                intent.setClassName(PACKAGENAME_ENG, CLASSNAME);
            }else if (checkPackage(PACKAGENAME_ENT)){
                intent.setClassName(PACKAGENAME_ENT, CLASSNAME);
            }else if (checkPackage(PACKAGENAME_I18N_TV)){
                intent.setClassName(PACKAGENAME_I18N_TV, CLASSNAME);
            }else if (checkPackage(PACKAGENAME_I18N)){
                intent.setClassName(PACKAGENAME_I18N, CLASSNAME); 
            }
            else
            {
                return false;
            }
            
//            intent.setClassName(PackageName.NORMAL, ClassName.NORMAL); 
      
            File file = new File(PATH);  
            if (file == null || !file.exists()) {  
                Toast.makeText(mActivity, "file does not exist", Toast.LENGTH_LONG).show();
                return false;  
            }  
      
            Uri uri = Uri.fromFile(file);  
            intent.setData(uri);  
            intent.putExtras(bundle);  
            try {  
                startActivity(intent);  
            } catch (ActivityNotFoundException e) { 
                Toast.makeText(mActivity, "open WPS failure", Toast.LENGTH_LONG).show();  
                e.printStackTrace();
                return false;  
            }  
            return true;  
    }



资料转载于: https://blog.csdn.net/u014452224/article/details/50560059

                        https://blog.csdn.net/houjianbo188/article/details/51403997

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android 应用程序中显示 PDF 文件,可以使用以下两种方式之一: 1. 使用 Google PDF Viewer Google PDF Viewer 是一款由 Google 开发的 PDF 阅读器,可以轻松地在 Android 应用程序中使用。要使用 Google PDF Viewer,需要在应用程序中使用 Intent 打开 PDF 文件,并指定 MIME 类型为 "application/pdf"。以下是一个示例代码: ```java File file = new File("/sdcard/mypdf.pdf"); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); ``` 请注意,上面的代码假设 PDF 文件位于 /sdcard/mypdf.pdf。 2. 使用第三方 PDF 阅读器库 除了 Google PDF Viewer,还有许多第三方 PDF 阅读器库可以使用。例如,AndroidPdfViewer 是一款受欢迎的开源库,可以轻松地在应用程序中显示 PDF 文件。 要使用 AndroidPdfViewer,需要添加以下依赖项: ```gradle dependencies { implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1' } ``` 然后,在布局文件中添加以下代码: ```xml <com.github.barteksc.pdfviewer.PDFView android:id="@+id/pdfView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 最后,在代码中加载 PDF 文件: ```java PDFView pdfView = findViewById(R.id.pdfView); pdfView.fromFile(new File("path/to/pdf")).load(); ``` 请注意,上面的代码假设 PDF 文件位于 "path/to/pdf"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值