如何从我的应用程序在MS-Excel或MS-Word - Android中打开excel,doc文件

问题描述 投票:3回答:4

我的要求是在我的应用程序中下载和查看Excel,Doc文件。所以我在手机存储中下载了Excel / Doc文件,并通过传递文件路径调用了ACTION_VIEW Intent。然后我收到此错误说“尝试将文件保存在设备上然后打开它”。

如果有人可以建议我打开excel或doc文件,我会很高兴。请大家为此我搜索了很多,到目前为止我没有找到解决方案,我确信我已经使用了正确的打开文件的意图。

我的代码在哪里:

Intent intentpdf = new Intent(Intent.ACTION_VIEW);
                intentpdf.setDataAndType(Uri.parse(message), "application/msword");
                intentpdf.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try {
                    mactivity.startActivity(intentpdf);
                } catch (ActivityNotFoundException e) {


                    if (!mactivity.isFinishing())
                        Toast.makeText(mactivity, "Install MSWord Viewer.", Toast.LENGTH_LONG).show();

                }


Intent intentpdf = new Intent(Intent.ACTION_VIEW);
                intentpdf.setDataAndType(Uri.parse(message), "application/vnd.ms-excel");
                intentpdf.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try {
                    mactivity.startActivity(intentpdf);
                } catch (ActivityNotFoundException e) {


                    if (!mactivity.isFinishing())
                        Toast.makeText(mactivity, "Install Excel Viewer.", Toast.LENGTH_LONG).show();

                }
android excel android-intent
4个回答
1
投票

您需要标志FLAG_ACTIVITY_NO_HISTORY,FLAG_GRANT_READ_URI_PERMISSION,FLAG_GRANT_WRITE_URI_PERMISSION。

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);


0
投票

我有这个完全相同的问题,我修复了它。问题很可能与您的ContentProvider / FileProvider实现有关。我的Excel表格在Google表格中打开正常,但我在MS Excel中收到此错误。解决方法是从提供程序返回正确的内容类型(通过getType()方法)。返回错误的内容类型将导致错误。

尝试使用此代码来查看它是否有用......如果有,则可以完全实现所有类型文件的例程。

    @Override
    public String getType(Uri uri) {
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    }

祝好运!


0
投票

尝试从文件中获取mime类型

public String getMimeTypeByFile(String filePath) {
        MimeTypeMap type = MimeTypeMap.getSingleton();
        return type.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(filePath));
    }

0
投票

没有什么是有效的,我打算最后打开Doc,PPT,Excel文件我找到了更好的解决方案,请在这里查看代码

if(fileType.equalsIgnoreCase("doc") || fileType.equalsIgnoreCase("docx")) {
                            String str = "com.microsoft.office.word";

                            Uri pathe = Uri.fromFile(fileN);
                            Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_VIEW);
                            intent.setDataAndType(pathe, "application/msword");
                            PackageManager packageManager = activity.getPackageManager();
                            if (intent.resolveActivity(packageManager) != null) {
                                activity.startActivity(intent.createChooser(intent, "Choose app to open document"));
                            }
                            else
                            {
                                //Launch PlayStore
                                try {
                                    activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+str)));

                                } catch (android.content.ActivityNotFoundException n) {
                                    activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id="+str)));
                                }
                            }

结果如下:查看屏幕截图enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.