确定哪些程序包可以打开xls文件

时间:2019-06-18 17:04:16

标签: android xlsx xls

我有一个APP,可以进行系统调用以打开内部生成的xls文件。

首先,我需要我的应用程序来识别系统上安装的哪些应用程序可以让您打开此类文件。

2nd因为我的应用程序出于其他目的打开了xls和xlsx文件,所以我需要为此应用程序从可以打开此类文件的应用程序列表中排除。

使用此代码,我可以毫无问题地打开xls和xlsx文件。

    private boolean openXLS(File file) {

        TActivity activity = activityReference.get();
        if (activity == null || activity.isFinishing()) return false;

        Uri uri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            uri = FileProvider.getUriForFile(activity, activity.getPackageName() + ".fileprovider", file);
        }
        else {
            uri = Uri.fromFile(file);
        }

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setDataAndType(uri, "application/vnd.ms-excel");
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        try {
            activity.startActivity(intent);
            return true;
        } catch(ActivityNotFoundException e) {
            return false;
        }
    }

0 个答案:

没有答案