Android 打开office文档

应用中需要打开office文档,word,excel,pdf等。

方案一:用系统自带的office工具打开,预览

        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(this, FILE_PROVIDER, file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            uri = Uri.fromFile(file);
        }
        switch (prefix) {
            case Constant.UploadFileType.XLS:
                intent.setDataAndType(uri, "application/vnd.ms-excel");
                break;
            case Constant.UploadFileType.XLSX:
                intent.setDataAndType(uri, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                break;
            case Constant.UploadFileType.DOC:
                intent.setDataAndType(uri, "application/msword");
                break;
            case Constant.UploadFileType.DOCX:
                intent.setDataAndType(uri, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
                break;
            case Constant.UploadFileType.PDF:
                intent.setDataAndType(uri, "application/pdf");
                break;
        }

        //toast remind
        startActivity(intent);
        finishActivity();

 

 

你可能感兴趣的:(Android)