Android 打开word,excel,ppt,txt,html等文件有用户选择用何种手机上面已经安装有的工具打开

由于工作的需要,今天研究了一下如何打开手机上面的word文档,excel,ppt。刚开始的以为只要能调用wps office工具打开就行,如果手机上面没有wps office就提示用户自己去下载,可是当我已经实现功能的时候,结果需求并不是我想的那样,而是调用手机上面的有用户自己选择用何种工具打开。于是我就又研究一下如何让用户选择自己已经安装的工具打开。下面先上一段用wps office打开word ,excel,ppt的代码

// 专用wps工具打开word,excel,ppt
    public boolean openFile(String path) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString(OPEN_MODE, READ_ONLY);
        bundle.putBoolean(CLEAR_BUFFER, true);
        bundle.putBoolean(CLEAR_TRACE, true);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.setClassName("cn.wps.moffice_eng",
                "cn.wps.moffice.documentmanager.PreStartActivity");
        File file = new File(path);
        if (file == null || !file.exists()) {
            return false;
        }
        Uri uri = Uri.fromFile(file);
        System.out.println("uri----<" + uri);
        intent.setData(uri);
        intent.putExtras(bundle);
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

“cn.wps.moffice_eng”,”cn.wps.moffice.documentmanager.PreStartActivity”。cn.wps.moffice_eng这个包名需要自己根据自己安装的wps的包名进行填写的,有的是cn.wps.moffice,所以可以自行选择判断,具体的如何输出查看自己安装的wps的包名请看下面的install方法里面的代码,下面的代码是用于检查用户是否已经安装wps office工具

// 判断是否安装wps
    public void isInstall() {

        List<PackageInfo> list = getPackageManager().getInstalledPackages(
                PackageManager.GET_PERMISSIONS);

        StringBuilder stringBuilder = new StringBuilder();

        for (PackageInfo packageInfo : list) {
            stringBuilder.append("package name:" + packageInfo.packageName
                    + "\n");
            ApplicationInfo applicationInfo = packageInfo.applicationInfo;
            stringBuilder.append("应用名称:"
                    + applicationInfo.loadLabel(getPackageManager()) + "\n");
            if (packageInfo.permissions != null) {

                for (PermissionInfo p : packageInfo.permissions) {
                    stringBuilder.append("权限包括:" + p.name + "\n");
                }
            }
            stringBuilder.append("\n");
            if ("cn.wps.moffice_eng".equals(packageInfo.packageName)) {
                isInstall = true;
            }
        }
        System.out.println(isInstall);
    }

packageInfo.packageName此代码就可以查看手机上面所有应用的包名,需要自行输出查看,如:System.out.println(packageInfo.packageName);下面的代码是用于查看文件的结尾类型

    private int Invalid(File f) {
        String filename = f.getName().toLowerCase();
        if (filename.endsWith(".xls")) {
            return 0;
        } else if (filename.endsWith(".doc")) {
            return 1;
        } else if (filename.endsWith(".ppt")) {
            return 2;
        }
        return 3;
    }

下面是用wps office打开的ppt的效果图
这里写图片描述
下面的代码则是用于用户自己选择用何种自己已经安装的工具打开文件
效果图如下

这里写图片描述

public class OpenFiles {
     //android获取一个用于打开HTML文件的intent
       public static Intent getHtmlFileIntent(File file)
       {
           Uri uri = Uri.parse(file.toString()).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(file.toString()).build();
           Intent intent = new Intent("android.intent.action.VIEW");
           intent.setDataAndType(uri, "text/html");
           return intent;
       }
     //android获取一个用于打开图片文件的intent
       public static Intent getImageFileIntent(File file)
       {
           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(file);
           intent.setDataAndType(uri, "image/*");
           return intent;
       }
       //android获取一个用于打开PDF文件的intent
       public static Intent getPdfFileIntent(File file)
       {
         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(file);
         intent.setDataAndType(uri, "application/pdf");
         return intent;
       }
     //android获取一个用于打开文本文件的intent
     public static Intent getTextFileIntent(File file)
     {   
       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(file);
       intent.setDataAndType(uri, "text/plain");
       return intent;
     }

     //android获取一个用于打开音频文件的intent
       public static Intent getAudioFileIntent(File file)
       {
         Intent intent = new Intent("android.intent.action.VIEW");
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         intent.putExtra("oneshot", 0);
         intent.putExtra("configchange", 0);
         Uri uri = Uri.fromFile(file);
         intent.setDataAndType(uri, "audio/*");
         return intent;
       }
       //android获取一个用于打开视频文件的intent
       public static Intent getVideoFileIntent(File file)
       {
         Intent intent = new Intent("android.intent.action.VIEW");
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         intent.putExtra("oneshot", 0);
         intent.putExtra("configchange", 0);
         Uri uri = Uri.fromFile(file);
         intent.setDataAndType(uri, "video/*");
         return intent;
       }


       //android获取一个用于打开CHM文件的intent
       public static Intent getChmFileIntent(File file)
       {
         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(file);
         intent.setDataAndType(uri, "application/x-chm");
         return intent;
       }


     //android获取一个用于打开Word文件的intent
       public static Intent getWordFileIntent(File file)
       {
         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(file);
         intent.setDataAndType(uri, "application/msword");
         return intent;
       }
     //android获取一个用于打开Excel文件的intent
       public static Intent getExcelFileIntent(File file)
       {
         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(file);
         intent.setDataAndType(uri, "application/vnd.ms-excel");
         return intent;
       }
     //android获取一个用于打开PPT文件的intent
       public static Intent getPPTFileIntent(File file)
       {
         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(file);
         intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
         return intent;
       }
       //android获取一个用于打开apk文件的intent
       public static Intent getApkFileIntent(File file)
       {
           Intent intent = new Intent(); 
           intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
           intent.setAction(android.content.Intent.ACTION_VIEW); 
           intent.setDataAndType(Uri.fromFile(file),  "application/vnd.android.package-archive"); 
           return intent;
       }

}

以上代码是用于根据不同的文件类型选择不同的应用工具进行打开文件
文件类型 attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <array name="fileEndingImage">
        <item>.png</item>
        <item>.gif</item>
        <item>.jpg</item>
        <item>.jpeg</item>
        <item>.bmp</item>
    </array>
    <array name="fileEndingAudio">
        <item>.mp3</item>
        <item>.wav</item>
        <item>.ogg</item>
        <item>.midi</item>
    </array>
    <array name="fileEndingVideo">
        <item>.mp4</item>
        <item>.rmvb</item>
        <item>.avi</item>
        <item>.flv</item>
    </array>
    <array name="fileEndingPackage">
        <item>.jar</item>
        <item>.zip</item>
        <item>.rar</item>
        <item>.gz</item>
        <item>.apk</item>
        <item>.img</item>
    </array>
    <array name="fileEndingWebText">
        <item>.htm</item>
        <item>.html</item>
        <item>.php</item>
        <item>.jsp</item>
    </array>
    <array name="fileEndingText">
        <item>.txt</item>
        <item>.java</item>
        <item>.c</item>
        <item>.cpp</item>
        <item>.py</item>
        <item>.xml</item>
        <item>.json</item>
        <item>.log</item>
    </array>
    <array name="fileEndingWord">
        <item>.doc</item>
        <item>.docx</item>
    </array>
    <array name="fileEndingExcel">
        <item>.xls</item>
        <item>.xlsx</item>
    </array>
    <array name="fileEndingPPT">
        <item>.ppt</item>
        <item>.pptx</item>
    </array>
    <array name="fileEndingPdf">
        <item>.pdf</item>
    </array>

</resources>

用于检查要打开的文件的后缀是否在遍历后缀数组中

private boolean checkEndsWithInStringArray(String checkItsEnd,
            String[] fileEndings) {
        for (String aEnd : fileEndings) {
            if (checkItsEnd.endsWith(aEnd))
                return true;
        }
        return false;
    }

调用OpenFiles类返回的Intent,打开相应的文件

private void open(File currentPath) {
        if (currentPath != null && currentPath.isFile()) {
            String fileName = currentPath.toString();
            Intent intent;
            if (checkEndsWithInStringArray(fileName, getResources()
                    .getStringArray(R.array.fileEndingImage))) {
                intent = OpenFiles.getImageFileIntent(currentPath);
                startActivity(intent);
            } else if (checkEndsWithInStringArray(fileName, getResources()
                    .getStringArray(R.array.fileEndingWebText))) {
                intent = OpenFiles.getHtmlFileIntent(currentPath);
                startActivity(intent);
            } else if (checkEndsWithInStringArray(fileName, getResources()
                    .getStringArray(R.array.fileEndingPackage))) {
                intent = OpenFiles.getApkFileIntent(currentPath);
                startActivity(intent);

            } else if (checkEndsWithInStringArray(fileName, getResources()
                    .getStringArray(R.array.fileEndingAudio))) {
                intent = OpenFiles.getAudioFileIntent(currentPath);
                startActivity(intent);
            } else if (checkEndsWithInStringArray(fileName, getResources()
                    .getStringArray(R.array.fileEndingVideo))) {
                intent = OpenFiles.getVideoFileIntent(currentPath);
                startActivity(intent);
            } else if (checkEndsWithInStringArray(fileName, getResources()
                    .getStringArray(R.array.fileEndingText))) {
                intent = OpenFiles.getTextFileIntent(currentPath);
                startActivity(intent);
            } else if (checkEndsWithInStringArray(fileName, getResources()
                    .getStringArray(R.array.fileEndingPdf))) {
                intent = OpenFiles.getPdfFileIntent(currentPath);
                startActivity(intent);
            } else if (checkEndsWithInStringArray(fileName, getResources()
                    .getStringArray(R.array.fileEndingWord))) {
                intent = OpenFiles.getWordFileIntent(currentPath);
                startActivity(intent);
            } else if (checkEndsWithInStringArray(fileName, getResources()
                    .getStringArray(R.array.fileEndingExcel))) {
                intent = OpenFiles.getExcelFileIntent(currentPath);
                startActivity(intent);
            } else if (checkEndsWithInStringArray(fileName, getResources()
                    .getStringArray(R.array.fileEndingPPT))) {
                intent = OpenFiles.getPPTFileIntent(currentPath);
                startActivity(intent);
            }
        }
    }

点击下载源码如有什么不懂的地方,请加群 50208422或Android交流群 470707794

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值