Android 打开pdf 文件

卢和昶
2023-12-01


         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

 类似资料: